ios UITableView 单元格 textLabel 颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9733267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITableView cell textLabel color
提问by Guru
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:
我有一个简单的问题UITableViewCell
。我想要的是更改所选单元格的文本颜色。在我的cellForRowAtIndexPath
方法中,我设置:
cell.textLabel.textColor = [UIColor whiteColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.highlightedTextColor = [UIColor orangeColor];
If the selectionStyle
is UITableViewCellSelectionStyleNone
, highlightedTextColor
will not change.
So I use these two methods to set the text color:
如果 selectionStyle
是UITableViewCellSelectionStyleNone
, highlightedTextColor
则不会改变。所以我用这两种方法来设置文字颜色:
- (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.
它可以工作,但是在滚动 tableview 时,颜色会变回来。
回答by Guru
Got the solution by setting color in if (cell == nil) check
通过在 if (cell == nil) 检查中设置颜色来获得解决方案
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.textLabel.textColor = [UIColor whiteColor];
}
and
和
- (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];
}
Thanks
谢谢
回答by Deepukjayan
All that you have to do is
你所要做的就是
cell.textLabel.textColor = [UIColor whiteColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.highlightedTextColor = [UIColor orangeColor];
inside if(cell == nil)
内部 if(cell == nil)
that will work fine.
那会很好用。
回答by u626198
set a global : int m_selectedCell;
modify it in
设置一个全局:int m_selectedCell;
修改它
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
and
和
just add
只需添加
if(m_selectedCell == indexPath.row){
...
...
}else{
}
in
在
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
that's all !
就这样 !