Search code examples
iphoneinterface-buildercelltableviewuitableview

Reusable TableViewCell in Interface Builder WITH changeable labels?


How do I make a reusable TableViewCell in Interface Builder with changeable labels?

Is this even possible? From what I understand apple has been giving custom TableViewCell in Interface Builder some love lately, so this should be possible?

Ps. I know there are a lot of questions with answers about TableViewCell in IB, but I couldn't find anyone that made labels work.


Solution

  • You can change anything in a cell that is being re-used. To customize labels that you create in IB, you should set their tags in IB itself & fetch the label using the same tag.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        MyCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
        if(cell == nil)
        {
            cell = [[[MyCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        }
    
    
        // Configure the cell.
        //Do anything here with the labels. Even add or remove them.
        (UILabel*) label1 = (UILabel*)[cell viewWithTag:1];
        return cell;
    }