简体   繁体   中英

Change the Text Color of UILabel in UITableViewCell

I'm trying to change the text color of a label in the cell when Current Date == Facebook Birthday List Date.

In cellForRowAtIndexPath: id did this

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

This works to change the color but when I scroll it changes back to the old color. How can I fix this?

-(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)条件内部编写了上面的逻辑。

To rule out potential problems with data try replacing

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

with

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

Do you always see the colour of rows 0, 1 and 15 set properly? Could it be that totalbirthlistArray changes unintentionally between scrolls, say in [birthdaylist reloadData] call?

replace your cellForRowAtIndexPath with this.I think, this will solves your issue.

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

}

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