繁体   English   中英

在UITableViewCell中更改UILabel的文本颜色

[英]Change the Text Color of UILabel in UITableViewCell

我正在尝试在当前日期== Facebook生日列表日期时更改单元格中标签的text color

cellForRowAtIndexPath: id完成了

if([curdatemonthstring isEqualToString:[[totalbirthlistArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]])
{
    [nameLabel setTextColor:[UIColor purpleColor]];
}

这可以更改颜色,但是当我滚动时,它会变回旧颜色。 我怎样才能解决这个问题?

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


    static NSString *simpleTableidentifier=@"SimpleTableItem";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simpleTableidentifier];

    if (cell == nil)
    {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableidentifier]autorelease];

    }

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [[[cell contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];


    contentview = [[[UIView alloc]init] autorelease];
    [contentview setFrame:CGRectMake(0, 0,320,80)];


    //Display Friends Name
    nameLabel = [[[UILabel alloc] init]autorelease];
    [nameLabel setBackgroundColor:[UIColor clearColor]];
    [nameLabel setFrame:CGRectMake(76, 5, 200, 45)];
    [nameLabel setFont:[UIFont fontWithName:@"Politica" size:20]];
    [nameLabel setText:[[[monthdataArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]objectForKey:@"name"]];

    if([curdatemonthstring isEqualToString:[[totalbirthlistArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]])
    {
        [nameLabel setTextColor:[UIColor purpleColor]];
    }

    [contentview addSubview:nameLabel];

[cell.contentView addSubview:contentview];

    return cell;

    [birthdaylist reloadData];




}

因为tableview在滚动时会重用它的单元格,所以问题主要是因为您在if(!cell)条件内部编写了上面的逻辑。

要排除数据的潜在问题,请尝试更换

if([curdatemonthstring isEqualToString:[[totalbirthlistArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]])

if(indexPath.row == 0 || indexPath.row == 1 || indexPath.row == 15)

您是否总是看到第0、1和15行的颜色设置正确? 可能是totalbirthlistArray在滚动之间无意间发生了变化,例如在[birthdaylist reloadData]调用中?

以此替换您的cellForRowAtIndexPath。我认为,这将解决您的问题。

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

static NSString *simpleTableidentifier=@"SimpleTableItem";

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simpleTableidentifier];

if (cell == nil)
{

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableidentifier]autorelease];

contentview = [[[UIView alloc]init] autorelease];
[contentview setFrame:CGRectMake(0, 0,320,80)];


//Display Friends Name
nameLabel = [[[UILabel alloc] init]autorelease];
[nameLabel setBackgroundColor:[UIColor clearColor]];
[nameLabel setFrame:CGRectMake(76, 5, 200, 45)];
nameLabel.tag = 1;
[nameLabel setFont:[UIFont fontWithName:@"Politica" size:20]];
[nameLabel setText:[[[monthdataArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]objectForKey:@"name"]];


[contentview addSubview:nameLabel];

[cell.contentView addSubview:contentview];

}

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

UILabel *nameLabel = [cell.contentView viewWihtTag:1];
if([curdatemonthstring isEqualToString:[[totalbirthlistArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]])
{
    [nameLabel setTextColor:[UIColor purpleColor]];
}

return cell;

[birthdaylist reloadData]; //I don't what is the use of this.

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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