UITableView with two custom cells (multiple identifiers) UITableView with two custom cells (multiple identifiers) ios ios

UITableView with two custom cells (multiple identifiers)


* I've renamed some of your NIB/Class names for a better understanding. *

First, you should register each cells' NIB:

- (void)viewDidLoad{    [super viewDidLoad];    static NSString *CellIdentifier1 = @"ContentCell";    static NSString *CellIdentifier2 = @"SpaceCell";    UINib *nib = [UINib nibWithNibName:@"CellViewNIBName" bundle:nil];    [self.tableView registerNib:nib forCellReuseIdentifier:CellIdentifier1];    nib = [UINib nibWithNibName:@"CellSpaceNIBName" bundle:nil];    [self.tableView registerNib:nib forCellReuseIdentifier:CellIdentifier2];    self.contentView.hidden = YES;    [self loadData];}

Because you have the NIBs registered, dequeueReusableCellWithIdentifier: will always return a cell:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath      *)indexPath{    static NSString *CellIdentifier1 = @"ContentCell";    static NSString *CellIdentifier2 = @"SpaceCell";    // Space Cell    if (indexPath.row % 2 == 1) {        CellSpace *cell = (CellSpace *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];        return cell;    }    // Content cell    else {        CellView *cell = (CellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];        // Configure cell        return cell;    }}

Last, but not least, make sure to implement the following delegate method:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    // Space cell's height    if (indexPath.row % 2 == 1) {        return 20.0f;    }    // Content cell's height    else {        return 80.0f;    }}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewCell *returncell;    static NSString *cellIdentifier ;    if(indexPath.section == 0)    {        cellIdentifier = @"cell1";    }    else if (indexPath.section == 1)    {        cellIdentifier = @"cell2";    }    UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];    MapTableViewCell *myCustomCell = (MapTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];    if(indexPath.section == 0)    {        if(myCell == nil)        {            myCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];            getLocationBtn = [UIButton buttonWithType:UIButtonTypeCustom];            getLocationBtn.frame = CGRectMake(myCell.frame.origin.x,myCell.frame.origin.y+5 , 200, 30);            [getLocationBtn setTitle:@"your button title" forState:UIControlStateNormal];            [getLocationBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];            [getLocationBtn addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];            }            [myCell.contentView addSubview:getLocationBtn];        returncell = myCell;    }    else if (indexPath.section == 1)    {        if (myCustomCell == nil)        {            myCustomCell = [[MapTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];        }            myCustomCell.nearbyLocation.text = @"demo Text";        returncell = myCustomCell;    }    return returncell;}

//mycustom tablviewcell

import "MapTableViewCell.h"

@implementation MapTableViewCell@synthesize nearbyLocation;-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];    if(self)    {            self.backgroundColor = [UIColor groupTableViewBackgroundColor];        nearbyLocation = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 200, 30)];            [self addSubview:nearbyLocation];    }    return self;}@end

Best way to use number of custom cells with default cell


In addition for the answers provided, I want to emphasize on the Cell Identifier for each different custom cells must be different too.

For example custom cellA with identifier "Cell" and custom cellB with identifier "Cell2".


matomo