2011-07-05 39 views
1

我正嘗試使用多個標籤創建UITableViewCell。我一直在嘗試做類似的事情(將「Name」和「Phone Number」左對齊並放在兩行上,然後在單元格中右對齊「Age」,並且可能垂直居中。我嘗試了不同的UITableViewCellStyle樣式,但希望定製它,使單元格可以包含3個標籤,而不是隻有兩個。任何幫助將不勝感激。提前致謝!如何爲UITableView創建自定義單元格

+0

歡迎來到計算器!請隨時設置一個用戶名逗留一段時間/ – Moshe

+0

@Wolfcow的解釋是完全正確的,但如果你只是需要更多的細節,看看這個,我只是發現它和它的完美:http:// www。 appcoda.com/customize-table-view-cells-for-uitableview/ –

回答

3

您可以做的一件事就是在界面構建器中創建一個自定義的UITableViewCell,並添加所需的任何標籤等,並在您填充行時使用該單元格。

在您的.h

文件,你會需要一個IBOutlet UITableViewCell的yourCellName

在.m文件

您可以配置自定義單元格的方法如下所示:

-(void)setUpViewComponents{ 

     yourCellName.textLabel.text = @"some kind of text"; 

     yourCellName.textLabel2.text = @"some other text"; 


     //and so on 

} 

然後在此方法中,您可以使用自定義單元填充表格

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



     static NSString *CellIdentifier = @"Cell"; 

     UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cell == nil) { 

       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 

     } 

     cell = yourCellName; //you can make additional 
          //changes to the cell here if you want 


    return cell; 
    } 

對不起,我希望它至少能幫助你開始。

0

創建延伸的UITableViewCell一個新的類,我給你舉個例子:

@interface YourCustomCell : UITableViewCell { 
    UILabel *lb1,*lb2,*lb3; 
} 


@implement YourCustomCell 

-(id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier{ 
    if (self=[super initWithFrame:frame reuseIdentifier:reuseIdentifier]){ 
      lb1=[UILabel alloc] initWithFrame:...]; 
      [self.contentView addSubview:lb1]; 
      ... 
    } 
    return self; 
}