Search code examples
objective-cuitableviewxibreuseidentifier

Change CellReuseIdentifier Based on section


I have a UITableView and a number of .xibs with different table view styles. Each .xib has it's own class and is registered with a reuse identifier based on the section. For example: In my viewDidLoad I have the following:

UINib *cellStyleOne = [UINib nibWithNibName:@"CellStyleOne" bundle:nil];
[self.tableView registerNib:cellStyleOne forCellReuseIdentifier:@"CellStyleOne"];

UINib *cellStyleTwo = [UINib nibWithNibName:@"CellStyleTwo" bundle:nil];
[self.tableView registerNib:cellStyleTwo forCellReuseIdentifier:@"CellStyleTwo"];

UINib *cellStyleThree = [UINib nibWithNibName:@"CellStyleThree" bundle:nil];
[self.tableView registerNib:cellStyleThree forCellReuseIdentifier:@"CellStyleThree"];

How would I assign each one of these reuse identifiers to a section of cells in my cellForRowAtIndexPath without causing any issues?

Thanks.


Solution

  • you can return any cell you like inside cellForRowAtIndexPath:, just do something like

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (indexPath.section == 0)
        {
            //create and return a cell with reuse ID: @"CellStyleOne"
        }
        else 
        {
            //create and return a cell using a different reuse ID
        }
    }