2016-03-08 63 views
0

我有一個自定義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]; 
    } 
} 
+1

你肯定你的if語句的工作? cell.textLabel.text是一個字符串,所以肯定比較應該是isEqualToString? – Gismay

+0

@Gismay這是一個很好的觀點。我剛剛嘗試過;看到我上面編輯的 – narner

+0

是self.myCellTextValue是靜態的嗎? –

回答

1

您可以使用selectedRowIndex作爲類級別的變量,並且每次選擇某行時都會繼續更新。開始 - 無論是選擇或不 - 這應該是0,所以,第一行是不同

我想你不想使用的文本價值,因爲它可能不是唯一

當你發生什麼事多選而不離開視圖?大概你需要清除舊行的白色文本,並將其重新設置爲新的文本?

最簡單的實現方法是重新加載每個選擇的tableView - 但如果這需要太長時間,您可以隨時重新加載單個行 - 在更新selectedRowIndex之前對行選擇設置selectedRowIndexPrevious,並重新加載這兩者行。上一行將在灰色重繪,並在新一白

這裏是你如何可能實現一些這

class MyViewController : UIViewController 
{ 
    // define the variables to keep track of row selection here 
    var selectedRowIndex : Int = 0 
    var selectedRowIndexPrevious : Int = -1 

    // the rest of your code 

,然後你需要更新selectedRow變量

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     // Uncheck the previous checked row 
     selectedRowIndexPrevious = selectedRowIndex 

     // **UPDATED** need to set the selectedRowIndex 
     selectedRowIndex = indexPath.row 
     // **UPDATED** 

     // reload needs an array of indexPath 
     // so we can supply the previous selection AND the current one 

     NSIndexPath* rowToReloadPrevious = [NSIndexPath indexPathForRow: selectedRowIndexPrevious inSection:0]; 
     NSIndexPath* rowToReloadNew = [NSIndexPath indexPathForRow: selectedRowIndex inSection:0]; 
     NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReloadPrevious, rowToReloadNew, nil]; 
     [tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone]; 
    } 

內cellForRowAtIndexPath,您只需要查看selectedRowIndex而不是檢查文本

if (indexPath.row == selectedRowIndex) { 
    cell.textLabel.textColor = [UIColor whiteColor]; 
} else { 
    cell.textLabel.textColor = [UIColor grey1Color]; 
} 

}

+0

嘿@Russell!迴應你關於文本價值不是唯一的觀點;那是個很好的觀點。那麼檢查每一行會更好嗎? 關於清除舊文本和重繪也是一個好點。 我不確定你使用'selectedRowIndex'作爲一個類級變量是什麼意思,那會是什麼樣子? – narner

+1

@narner - 我已經更新了答案。希望這給你你所需要的 – Russell

+0

謝謝,這是超級超級接近。現在雖然會發生什麼,但第一個單元總是白色的,第二個單元總是灰色的。也許需要一個'deselectRowForIndexPath'的地方? – narner

1

你可以這樣做:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //change your cell text color here 
    cell= [tableView cellForRowAtIndexPath:indexPath] 
    cell.textLabel.textColor = [UIColor whiteColor]; 
} 

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (cell.isSelected == YES) 
    { 
     cell.textLabel.textColor = [UIColor grey1Color]; 
    } 
    else 
    { 
     cell.textLabel.textColor = [UIColor whiteColor]; 
    } 
} 

另一種方式是繼承的tableview細胞並實現以下方法:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 
    [self updateTextColor:selected]; 
} 

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated { 
    [super setHighlighted:highlighted animated:animated]; 
    [self updateTextColor:highlighted]; 
} 

- (void)updateTextColor:(BOOL)isSelected { 
    labelA= //get reference of the cell textlabel 
    if (labelA) { 
     if (isSelected) { 
      [labelA setTextColor:[UIColor whiteColor]]; 
     } else { 
      [labelA setTextColor:[UIColor greyColor]]; 
     } 
    } 


} 
+0

謝謝你,這讓我更加接近!唯一不同的是,如果選擇,我將需要單元格爲白色;而不是灰色 現在發生的是如果我選擇一個單元格,它的文本變成白色,然後選擇另一個單元格,第一個單元格的文本保持白色而不是灰色。我缺少什麼東西? – narner

+0

我更新了答案...請檢查它@narner –