我有一个自定义UITableViewController
,它有一行,只有两个UITableViewCell
s。动态更改UITableViewCell文本的textColor
我想动态设置我UITableViewCell
的文本基于几件事情的颜色(cell.textLabel.textColor
):
1)如果这是第一次启动时,第一个单元格的文本颜色应该是[UIColor whiteColor]
和第二小区的文本颜色应该是[UIColor grey1Color]
2)如果用户选择的小区和离开屏幕,并随后返回到表视图,最后选择的单元格的文本颜色应该是[UIColor whiteColor]
,和的文本颜色没有选择的细胞应该是[UIColor grey1Color]
。
每当选择一个单元格时,属性都会更新; myCellTextValue
。这是为了在这个特定的表视图之外进行一些API调用。
我对实现上述逻辑的想法是使用此属性来确定单元格的文本应该是什么颜色。我下面的代码尝试在cellForRowAtIndexPath
:
if (cell.textLabel.text == self.myCellTextValue) {
cell.textLabel.textColor = [UIColor whiteColor];
} else {
cell.textLabel.textColor = [UIColor grey1Color];
}
}
然而,这两种细胞的文本颜色始终是灰色的。我敢肯定这主要是因为误解了某些地方的创作。有没有人有任何关于如何正确实施这个指针?谢谢!
编辑:以下@ Gismay的评论,我尝试了下面的代码;但得到了同样的结果:
if ([cell.textLabel.text isEqualToString:self.myCellTextValue]) {
cell.textLabel.textColor = [UIColor whiteColor];
} else {
cell.textLabel.textColor = [UIColor grey1Color];
}
编辑2:我也试过在检查包裹上面的代码,以确保我们只盯着每次一个单元,但这并没有影响之一:
if((indexPath.section==0)&&(indexPath.row==0)){
if ([cell.textLabel.text isEqualToString:self.myCellTextValue]) {
cell.textLabel.textColor = [UIColor whiteColor];
} else {
cell.textLabel.textColor = [UIColor grey1Color];
}
} else if((indexPath.section==0)&&(indexPath.row==1)){
if ([cell.textLabel.text isEqualToString:self.myCellTextValue]) {
cell.textLabel.textColor = [UIColor whiteColor];
} else {
cell.textLabel.textColor = [UIColor grey1Color];
}
}
你肯定你的if语句的工作? cell.textLabel.text是一个字符串,所以肯定比较应该是isEqualToString? – Gismay
@Gismay这是一个很好的观点。我刚刚尝试过;看到我上面编辑的 – narner
是self.myCellTextValue是静态的吗? –