3

我目前在我的 UITableView 上有以下代码用于我的 cellForRowAtIndexPath:

- (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 节中的所有单元格都需要有红色的 nameLabels,第 5 节中的所有单元格都需要有绿色的 valueLabels。但是,如果我在初始化所有标签的文本后将其插入代码中:

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

然后一切都变得一团糟,桌子全是小故障,文字在奇怪的地方,标签颜色错误,看起来有些随意。

如何为表格中的每个单元格指定这三个标签的颜色?

4

3 回答 3

0

你的代码似乎没问题。我唯一可以建议尝试的是不要使用autorelease标签,而是release在添加到 contentView 后立即使用。

于 2011-01-14T04:21:01.223 回答
0

问题发生了,因为UITableViewCells 被 system 随机回收。这就是为什么您还需要设置单元格默认值:

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

顺便说一句,您的单元格看起来很复杂,我建议使用 Interface Builder 创建它。

于 2011-01-14T04:21:51.450 回答
0

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

尝试这个,

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);
}
于 2011-01-14T07:34:14.130 回答