ios - 如何为不同的 UITableView 部分使用不同的 TableView 单元格类

标签 ios objective-c uitableview tableview

我能做到吗?

if (indexpath.section == 0) {
    // Use Class 1
} else if (indexpath.section == 1) {
    // Use Class 2
} 

我试过了但是不行

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        OneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"One" forIndexPath:indexPath];
        if( cell == nil){
            cell = [[OneTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"One"];
        }
        cell.oneLabel.text = @"HAHAHA";
        return cell;
    }else{
        TwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Two" forIndexPath:indexPath];
        if( cell == nil){
            cell = [[TwoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Two"];
        }
        cell.twoLabel.text = @"HEHEHE";
        return cell;
    }
}

最佳答案

从您显示的代码来看,您的 oneLabeltwoLabel 从未被初始化。如果您想快速修复,可以将它们都替换为 textLabel。如下所示,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        OneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"One" forIndexPath:indexPath];
        if( cell == nil){
           cell = [[OneTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"One"];
        }
        cell.textLabel.text = @"HAHAHA";  // Modified
        return cell;
    } else {
        TwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Two" forIndexPath:indexPath];
        if( cell == nil){
            cell = [[TwoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Two"];
        }
        cell.textLabel.text = @"HEHEHE";  // Modified
    }
}

您将能够在表格 View 中看到两个不同部分的不同文本。它们确实是不同的 TableviewCell 类。

但是,如果您想为不同的 UITableViewCell 使用不同的标签,那么您必须确保它们在某处以某种方式进行了初始化。例如,您可以覆盖自定义表格 View 单元格中的默认 UITableviewCell 初始值设定项。例如,在您的 OneTableViewCell.m 文件中,在 @implementation@end 之间添加以下内容。在这种情况下,您可以在 UITableView 类中使用您的原始代码。

@implementation OneTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style
              reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if ( self ) {
          _oneLabel = [[UILabel alloc] init];
          [self.view addSubView:self.oneLabel];
    }
    return self; 
}

@end

关于ios - 如何为不同的 UITableView 部分使用不同的 TableView 单元格类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28394711/

相关文章:

ios - 如何自定义 xAxis 下的标签颜色?

ios - 如何将 UISwitch 嵌入到静态 UITableView 中?

ios - 有没有办法断点所有按钮的 touchUpInside 方法?

ios - 如何从 UITableView 应用程序的详细 View 中删除当前行?

ios - 在 UITableviewCell 中布局动态 subview ,对边距进行约束

ios map 捕捉到道路实现

javascript - 如何在 ARchitectBrower.html Wikitude SDK for ios 中使用 AR.ImageResource 设置 HTML img src 值

ios - 如何在 Swift 中为协议(protocol)编写单元测试

ios - NSCondition 或 @synchronized

objective-c - 检查条件不起作用并返回无法识别的选择器