Lawrence Tan
Lawrence Tan

Reputation: 507

How to use different table view cell class for different UITableView sections

Can I achieve this?

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

I tried this but not working

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

Upvotes: 1

Views: 776

Answers (1)

Yuchen
Yuchen

Reputation: 33036

From the code that you show, your oneLabel and twoLabel are never initialized. If you want a quick fix, you can replace both of them with textLabel. Something as the following,

- (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
    }
}

And you will be able to see different text for two different sections in the table view. And they are indeed different TableviewCell classes.

However, if you want to use different labels for different UITableViewCell, then you have to make sure that they are initialized somehow somewhere. For example, you could overwrite the default UITableviewCell initializer in your custom table view cell. For example, in your OneTableViewCell.m file, add the following between @implementation and @end. In this case, you can use your original code in the UITableView class.

@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

Upvotes: 1

Related Questions