Pheepster
Pheepster

Reputation: 6357

UILabel does not render in prototype cell

I have a UIViewController that contains a UITableView. In my table view I have a prototype cell in which I want to place custom labels. I cannot get any of my custom labels to render in the cell. The datasource and tableview delegates are connected and functioning properly. For my first attempt, I dragged a UILabel control onto my prototype cell. I set its tag to 1000. Here is my code to populate the cells:

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    UILabel *customLbl = (UILabel*)[cell viewWithTag:1000];
    if(indexPath.section == 0){
        [cell.textLabel setText:[rData.currentHeightReadings objectForKey:[heightKeys objectAtIndex:indexPath.row]]];
        [customLbl setText:@"CFS"];
    } else {
        [cell.textLabel setText:[rData.currentFlowReadings objectForKey:[flowKeys objectAtIndex:indexPath.row]]];
        [customLbl setText:@"Ft."];
    }

    return cell;
}

The default textLabel for each cell renders as it should. However, the custom label does not appear. I verified that the prototype cell was actually rendering by changing the background color of the cell's content view. In my story board I have set the prototype cell's reuse identifier to match my code. The style is set to 'Custom.' What else should I be doing? Thanks!

Upvotes: 0

Views: 172

Answers (3)

mityaika07
mityaika07

Reputation: 653

Try this:

UILabel *customLbl = (UILabel*)[cell.contentView viewWithTag:1000];

Upvotes: 0

Andy Obusek
Andy Obusek

Reputation: 12842

Verify that your autolayout constraints are correct such that the UILabel is really positioned where you expect it.

Upvotes: 1

Sergey Grishchev
Sergey Grishchev

Reputation: 12051

I had the same issue once and I've solved it by not calling any methods to cell.textLabel property. Just don't set its text in any way.

Try it out and report back.

Upvotes: 0

Related Questions