UITableViewCell内容视图-添加UILabels [英] UITableViewCell Content View - Adding UILabels

查看:55
本文介绍了UITableViewCell内容视图-添加UILabels的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在UITableView上为cellForRowAtIndexPath使用以下代码:

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...

}

但是我想做的是,根据单元格的不同,使这三个标签的颜色不同.例如,第3节中的所有单元格都需要具有红色的nameLabel,而第5节中的所有单元格都需要具有绿色的valueLabel.但是,如果我在初始化所有标签的文本后将其插入代码中,则:

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?

推荐答案

每次回收单元时,您都必须重置两个标签的 textColor

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

尝试一下,

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

这篇关于UITableViewCell内容视图-添加UILabels的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆