2011-09-24 47 views
0

我已经成功地使用自定义UITableViewCells使用单独的类,但我一直在想有一个更简单的方法,所以我尝试了以下。表格构建,但单元格是空白的(所以大概我的自定义单元格不能正确显示)。任何想法赞赏。TableViewCell是否必须有自己的类?

我的文件有:

@interface RemediesPopUp : UIViewController <UITableViewDataSource, UITableViewDelegate>{ 

    UITableView *remediesDataTable; 
    UITableViewCell *remediesTableCell; // ** custom cell declared here ** 

    NSArray *remediesArray; 

} 

表格控件:

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

    static NSString *CellIdentifier = @"Cell"; 

    remediesTableCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (remediesTableCell == nil) { 
     remediesTableCell = [[[UITableViewCell alloc] init] autorelease]; 
    } 

    // Configure the cell. 

    return remediesTableCell; 

} 

我拖着一个自定义单元格对象到XIB文件窗格(所以我有视图和一个单独的自定义单元格显示)。我将自定义单元连接到文件管理器。为了测试,我将一些(静态)标签粘贴到自定义单元格上,以便看看是否正在加载。

页面和表格加载得很好,但单元格是空白的(白色)。

任何想法?

回答

1

首先,确保您的自定义单元格是自定义子类(UITableViewCell的子类)并且具有自己的nib文件(例如,不要将其与您的表视图控制器集成)。

在笔尖中,选择身份检查器并确保您的单元格的类是CustomCell(或任何您称之为的)。还要确保在属性检查器下,您将其Identifier设置为“CustomCellIdentifier”或类似。

在您的表视图的数据源,您tableView:cellForRowAtIndexPath方法应该包含类似的代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier"; // Same as you set in your nib 

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier]; 
    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 

     for (id oneCell in nib) { 
      if ([oneObject isKindOfClass:[CustomCell class]]) { 
       cell = (CustomCell *)oneCell; 
      } 
     } 
    } 

    // Customise the labels etc... 

    return cell; 
} 

这可以确保您的自定义单元格是从自己的笔尖直接加载,让您直观地布置任何意见或标签。这些标签的文本可以在适当的单元出列/创建后设置。

在你的代码中,你的'自定义'单元格只是一个普通的UITableViewCell。要使用界面构建器自定义单元格,您需要创建自己的自定义子类。

EDIT

对于自定义单元,其仅使用一个笔尖(和定制子类),改变上述代码例如将细胞转换为普通UITableViewCell,即改变CustomCellUITableViewCell 。在Interface Builder中为每个单元格的自定义视图(标签等)分配一个唯一的标签。

有了到位,代码应该是这个样子:

#define MyLabelTag 10 // These should match the tags 
#define MyViewTag 11 // assigned in Interface Builder. 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier"; // Same as you set in your nib 

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier]; 
    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 

    for (id oneCell in nib) { 
     if ([oneObject isKindOfClass:[UITableViewCell class]]) { 
      cell = (UITableViewCell *)oneCell; 
     } 
    } 
} 

    // Get references to the views/labels from the tags. 
    UILabel *myLabel = (UILabel *)[cell viewWithTag:MyLabelTag]; 
    UIView *myView = (UIView *)[cell viewWithTag:MyViewTag]; 

    // Customise the views/labels... 
    // ... 

    return cell; 
} 
+0

+1,但不需要将单元格设置为自定义子类。你的xib可以包含你喜欢的任何子视图,你可以通过标签来引用它们。 – jrturton

+0

@ jrturton:的确如此,尽管我个人认为如果您可以通过出口而不是通过IB中指定的任意标签来引用自定义单元格标签,它会让事情变得更加清晰。也许是个人喜好。 – Stuart

+0

谢谢你们。我认为杰顿的想法更符合我的想法。我相信我不需要一个自定义的子类。有人可以用一些建议的代码扩展这种想法。谢谢 – Jeremy

0

我通常所做的就是将UIView放入UITableViewCell中,然后将所有标签和其他UI材质放在该UIView中。如果这不起作用,请告诉我。我可能有另一种解决方案。

+0

Mohabitar嗨。请参阅上面的评论。我试图不使用子类,但我不知道如何引用自定义单元格中的对象。如果您有建议的代码,将非常感谢。谢谢。 – Jeremy

相关问题