close

Iphone在table cell中添加自訂佈局view

from http://fecbob.pixnet.net/blog/post/35424051

在android開發listView中,每一行的清單可以通過相應的xml定義視圖。在iphone開發中,tableView也提供了通過nib自訂視圖的解決方案。這就使開發者能夠完成相當複雜的介面佈局。

 

下麵介紹table中添加自訂的table cell。實現的效果如下:


image_thumb6.png  

實現過程很簡單,首先創建一個table視圖,添加table相應的協定。這一步很簡單,在這裡就不寫如何實現的了。不懂的可以看我以前的博客,或者看原始程式碼。

 

接下來,新建檔 並在 subclass 裡 選擇 UITableViewCell 這裡我命名為 “MyCell”

 

然後在利用IB創建一個名為mycell的nib,在裡面拖入一個UITableViewCell並將其類名改為MyCell。

image_thumb7.png  

-(NSInteger) tableView:(UITableView *)tableView

numberOfRowsInSection:(NSInteger)section


    return 1; 
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

    static NSString *CellIdentifier = @"CustomCellIdentifier"; 
    MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"mycell" owner:self options:nil]; 
        cell = [array objectAtIndex:0]; 
        [cell setSelectionStyle:UITableViewCellSelectionStyleGray]; 
    } 
    [[cell lable] setText:@"你好"]; 
    //[[cell imageView] setImage:[UIImage imageNamed:[imageNameArray objectAtIndex:indexPath.row]]]; 
    //[[cell nameLabel] setText:[nameArray objectAtIndex:indexPath.row]]; 
    return cell; 

- (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  

{       
    return 90; 
}

arrow
arrow
    全站熱搜

    zer931 發表在 痞客邦 留言(0) 人氣()


    留言列表 留言列表

    發表留言