Iphone TableViewCell必须有自己的类吗?

Iphone TableViewCell必须有自己的类吗?,iphone,uitableview,Iphone,Uitableview,我已经使用单独的类成功地使用了自定义UITableViewCells,但我一直认为有一种更简单的方法,所以我尝试了以下方法。表格将生成,但单元格为空(因此我的自定义单元格可能未正确显示)。任何想法都值得赞赏 我的档案是: @interface RemediesPopUp : UIViewController <UITableViewDataSource, UITableViewDelegate>{ UITableView *remediesDataTable; UI

我已经使用单独的类成功地使用了自定义UITableViewCells,但我一直认为有一种更简单的方法,所以我尝试了以下方法。表格将生成,但单元格为空(因此我的自定义单元格可能未正确显示)。任何想法都值得赞赏

我的档案是:

@interface RemediesPopUp : UIViewController <UITableViewDataSource, UITableViewDelegate>{

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

    NSArray *remediesArray;

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

页面和表格加载正常,但单元格为空白(白色)


有什么想法吗?

我通常会在UITableViewCell中放置一个UIView,然后将所有标签和其他UI内容放在该UIView中。如果这不起作用,请告诉我。我可能有另一个解决方案。

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

在nib中,选择identity inspector并确保单元格的类为
CustomCell
(或您所称的任何类)。还要确保在属性检查器下将其
标识符设置为“CustomCellIdentifier”或类似

在表视图数据源中,您的
tableView:cellforrowatinexpath
方法应包含以下类似代码:

- (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;
}
这样可以确保自定义单元格直接从其自己的nib加载,从而使您可以直观地布局任何视图或标签。一旦相应的单元格被取消排队/创建,就可以设置这些标签的文本

在代码中,“自定义”单元格只是一个普通的
UITableViewCell
。要使用interface builder自定义单元格,您需要创建自己的自定义子类。

编辑

对于仅使用nib(而非自定义子类)的自定义单元格,更改上述代码示例以将单元格强制转换为普通的
UITableViewCell
,即更改
CustomCell
UITableViewCell
。在界面生成器中,为每个单元格的自定义视图(标签等)指定一个唯一的标记

有了它,代码应该如下所示:

#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;
}

+1,但不需要将单元格设为自定义子类。您的xib可以包含您喜欢的任何子视图,并且您可以通过标记来引用它们。@jrturton:是的,但我个人认为,如果您可以通过插座而不是IB中指定的任意标记来引用自定义单元格标签,这会让事情变得更清楚。也许是个人偏好。谢谢大家。我认为jrturton的想法更符合我的想法。我相信我不需要自定义子类。有人能用一些建议的代码来扩展这种想法吗。Thanks@Jeremy:我根据jrturton的建议添加了一个新示例,只使用nib和view标记。谢谢StuDv。我试试看。谢谢,你好,莫哈比塔。请看上面的评论。我试图不使用子类,但我不确定如何引用自定义单元格中的对象。如果您建议使用代码,我们将不胜感激。谢谢
#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;
}