Search code examples
uitableviewidentifier

Custom TableViewCell with own different identifier


I have tableview with custom prototype cells generated from array

cellLabel = [[NSArray alloc] initWithObjects:
             @"20 Последних новостей",
             @"Политика",
             @"Экономика",
             @"Право",
             @"Происшествия",
             @"Культура",
             @"Здоровье",
             @"Технологии",
             @"В Мире",
             @"Калейдоскоп",
             nil];

My question is How I can set own different reuseIdentifier for each cell while creating cells Or I need to do 10 different cells?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellId = @"Sections";

sectionCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellId];

if (!Cell) {

    Cell = [[sectionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellId];

}

Cell.sectionTitle.text = [cellLabel objectAtIndex:indexPath.row];


return Cell;

}

thanks


Solution

  • I'm not sure I would recommend using different reuse identifiers for every dynamically generated cell, but if you wanted, you could do something like the following:

     switch (indexPath.row) {
            case 0:
                cell = [tableView dequeueReusableCellWithIdentifier:@"1"];
                break;
            case 1:
                cell = [tableView dequeueReusableCellWithIdentifier:@"2"];
    
            default:
                break;
        }
    cell.textLabel.text = [cellLabel objectAtIndex:indexPath.row];