我有一個自定義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是靜態的嗎? –