简体   繁体   中英

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?

Your code seems ok. The only thing I can suggest trying is not using autorelease for the labels and instead release each immediately after adding to contentView.

The problem happens, because UITableViewCells are randomly recycled by system . That's why you need to set also cell default values:

if(indexPath.section==3)
    nameLabel.textColor = [UIColor redColor];
else
    nameLabel.textColor = [UIColor blackColor];

Btw your cell looks so complex, I'd recommend creating it with Interface Builder.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM