ios UITableViewCell 选中行的文字颜色变化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5841056/
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
UITableViewCell selected row's text color change
提问by Maulik
I am having a tableview and I want to know that how can I change the selected row's text color, say to Red ? I tried by this code :
我有一个tableview中,我想知道我怎么能更改所选行的文本颜色,说红?我用这个代码试过:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell= [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
cell.text = [localArray objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
cityName = [localArray objectAtIndex:indexPath.row];
UITableViewCell* theCell = [tableView cellForRowAtIndexPath:indexPath];
theCell.textColor = [UIColor redColor];
//theCell.textLabel.textColor = [UIColor redColor];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
(1) When I select any row then text color changed to the red but when I select another then previously selected row's text remains red. how can I solve this ?
(1)当我选择任何行则文本颜色变为红色,但是当我选择另一则先前选定行的文本仍然是红色的。我该如何解决这个问题?
(2) When I scroll the table text color change to the black color how to solve this ?
(2) 当我滚动表格文本颜色变为黑色时如何解决这个问题?
Thanks..
谢谢..
回答by Ole Begemann
Do this in tableView:cellForRowAtIndexPath:
:
这样做tableView:cellForRowAtIndexPath:
:
cell.textLabel.highlightedTextColor = [UIColor redColor];
(And don't use cell.text = ...
anymore. It has been deprecated for almost 2 years now. Use cell.textLabel.text = ...
instead.)
(不要再使用cell.text = ...
了。它已经被弃用了将近 2 年。cell.textLabel.text = ...
改用。)
As Raphael Oliveiramentioned in the comments, if the selectionStyle of your cell equals UITableViewCellSelectionStyleNone
this won't work. Check Storyboard as well for the selection style.
正如Raphael Oliveira在评论中提到的,如果您的单元格的 selectionStyle 等于UITableViewCellSelectionStyleNone
这将不起作用。检查 Storyboard 以及选择样式。
回答by Rinku
If you want to change the text colour only, with out changing cell background colour. you can use this.Write this code in in cellForRowAtIndexPath method
如果您只想更改文本颜色,而不更改单元格背景颜色。你可以使用 this.Write 在 cellForRowAtIndexPath 方法中的这段代码
UIView *selectionColor = [[UIView alloc] init];
selectionColor.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = selectionColor;
cell.textLabel.highlightedTextColor = [UIColor redColor];
回答by Ranferi Dominguez
I had the same problem, try this!
我遇到了同样的问题,试试这个!
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
for (id object in cell.superview.subviews) {
if ([object isKindOfClass:[UITableViewCell class]]) {
UITableViewCell *cellNotSelected = (UITableViewCell*)object;
cellNotSelected.textLabel.textColor = [UIColor blackColor];
}
}
cell.textLabel.textColor = [UIColor redColor];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
That may be the solution to your (and mine) problem.
这可能是您(和我的)问题的解决方案。
回答by mikeho
If you're already subclassing UITableViewCell
, it's easier/cleaner to set the colors in the awakeFromNib
method (assuming you're instantiating from a storyboard or xib).
如果您已经子类化UITableViewCell
,则在awakeFromNib
方法中设置颜色会更容易/更清晰(假设您是从故事板或 xib 实例化)。
@implementation MySubclassTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.frame];
self.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:0.1 green:0.308 blue:0.173 alpha:0.6];
self.customLabel.highlightedTextColor = [UIColor whiteColor];
}
@end