UITableView单元格textLabel颜色 [英] UITableView cell textLabel color

查看:102
本文介绍了UITableView单元格textLabel颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题 UITableViewCell
我想要的是更改所选单元格的文本颜色。
在我的 cellForRowAtIndexPath 方法中,我设置:

I have a simple issue with UITableViewCell. What I want is to change the text color of a selected cell. In my cellForRowAtIndexPath method, I set:

cell.textLabel.textColor = [UIColor whiteColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.highlightedTextColor = [UIColor orangeColor];

如果 selectionStyle UITableViewCellSelectionStyleNone highlightedTextColor 不会改变。
所以我使用这两种方法设置文本颜色:

If the selectionStyle is UITableViewCellSelectionStyleNone, highlightedTextColor will not change. So I use these two methods to set the text color:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  [tableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor orangeColor];    
  return indexPath;
}
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
  [tableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor whiteColor];    
  return indexPath;
}

它可以工作,但滚动桌面视图时,颜色会变回。

It works, but when scrolling the tableview, the color changes back.

推荐答案

通过在if(cell == nil)中设置颜色来获得解决方案
检查

Got the solution by setting color in if (cell == nil) check

if (cell == nil) 
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    cell.textLabel.textColor = [UIColor whiteColor];  
}     

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor orangeColor];

}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
        [tableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor whiteColor];

}

谢谢

这篇关于UITableView单元格textLabel颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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