Search code examples
iphoneiosuitableviewuicolor

UITableViewCell Content View - Adding UILabels


I currently have the following code for my cellForRowAtIndexPath on my UITableView:

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

    static NSString* CellIdentifier = @"Cell";
    UILabel* nameLabel = nil;
    UILabel* valueLabel = nil;
    UILabel *percentLabel = nil;

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
    if ( cell == nil ) 
    {
        inthere = YES;
        cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault 
                                       reuseIdentifier: CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        nameLabel = [[[UILabel alloc] initWithFrame:CGRectMake( 7.0, 0.0, 140.0, 44.0 )] autorelease];
        nameLabel.tag = 21;
        nameLabel.font = [UIFont systemFontOfSize: 12.0];
        nameLabel.textAlignment = UITextAlignmentLeft;
        nameLabel.textColor = [UIColor darkGrayColor];
        nameLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
        nameLabel.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview: nameLabel];

        valueLabel = [[[UILabel alloc] initWithFrame: CGRectMake( 165.0, 0.0, 80, 44.0 )] autorelease];
        valueLabel.tag = 22;
        valueLabel.font = [UIFont systemFontOfSize: 11];
        valueLabel.textAlignment = UITextAlignmentRight;
        valueLabel.textColor = [UIColor blueColor];
        valueLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
        valueLabel.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview: valueLabel];

        percentLabel = [[[UILabel alloc] initWithFrame: CGRectMake(245, 0.0, 65, 44.0 )] autorelease];
        percentLabel.tag = 24;
        percentLabel.font = [UIFont systemFontOfSize: 12];
        percentLabel.textAlignment = UITextAlignmentRight;
        percentLabel.textColor = [UIColor blueColor];
        percentLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
        percentLabel.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview: percentLabel];
    }
    else
    {
        nameLabel = (UILabel*)[cell.contentView viewWithTag:21];
        valueLabel = (UILabel*)[cell.contentView viewWithTag:22];
        percentLabel = (UILabel *)[cell.contentView viewWithTag:24];
    }

   ...and then I initialize the text of each of these three labels...

}

But what I would like to do is have these three labels be different colors depending on the cell. For example, all the cells in section 3 need to have red nameLabels and all the cells in section 5 need to have green valueLabels. But if I insert this into the code after I initialize the text of all the labels:

if(indexPath.section==3) {
    nameLabel.textColor = [UIColor redColor];
}
if(indexPath.section==5) {
    valueLabel.textColor = [UIColor greenColor];
}

then everything gets messed up and the table is all glitchy, with text being in odd places and labels being wrong colors, and somewhat random looking.

How can I specify colors of these three labels for every single cell in my table?


Solution

  • You have to reset both the labels' textColor every time the cell is recycled

    Try this,

    if (indexPath.section == 3) {
    
        nameLabel.textColor = [UIColor redColor];
        nameLabel.frame = CGRectMake(someX, someY, someWidth, someHeight);
        valueLabel.textColor = [UIColor blackColor];
        valueLabel.frame = CGRectMake(someX, someY, someWidth, someHeight);
    }
    
    if (indexPath.section == 5) {
    
        nameLabel.textColor = [UIColor blackColor];
        nameLabel.frame = CGRectMake(someX, someY, someWidth, someHeight);
        valueLabel.textColor = [UIColor greenColor];
        valueLabel.frame = CGRectMake(someX, someY, someWidth, someHeight);
    }