2012-03-16 171 views
13

我有一個簡單的問題UITableViewCell。 我想要的是改變選定單元格的文字顏色。 在我cellForRowAtIndexPath方法,我設置:UITableView單元格textLabel顏色

cell.textLabel.textColor = [UIColor whiteColor]; 
cell.selectionStyle = UITableViewCellSelectionStyleNone; 
cell.textLabel.highlightedTextColor = [UIColor orangeColor]; 

如果selectionStyleUITableViewCellSelectionStyleNonehighlightedTextColor不會改變。 於是我就用這兩種方式來設置文本顏色:

- (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; 
} 

它的工作原理,但滾動的tableview時,顏色變回。

+0

怎麼樣聲明一個NSIndexPath * urIndexPath;在你的.h?然後在你的didSelecteRowAtIndexPath方法中,將所選單元格的indexPath傳遞給urIndexPath,然後重載數據以更新UI,在你的cellForRowAtIndexPath中,比如說,如果urIndexPath是== indexPath {set color to orange; } else {任何顏色默認情況下} – janusbalatbat 2012-03-16 07:13:22

+0

@Akhildas當你滾動tableView cellForRowAtIndex:方法將被調用always.But不是其他方法,你可以調用「WillSelectRowAtIndexPath」方法在你「cellForRowAtIndex:」方法來使它作品 – 2012-03-16 07:15:46

回答

19

在如果(細胞==零)檢查

if (cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
    cell.textLabel.textColor = [UIColor whiteColor]; 
}  

- (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]; 

} 

由於設置顏色得到了解決

14

所有你需要做的就是

cell.textLabel.textColor = [UIColor whiteColor]; 
cell.selectionStyle = UITableViewCellSelectionStyleNone; 
cell.textLabel.highlightedTextColor = [UIColor orangeColor]; 

內,如果(細胞==零)

,將正常工作。

0

設置一個全球性的:int m_selectedCell; 修改它

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

只需添加

if(m_selectedCell == indexPath.row){ 
... 
... 
}else{ 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath 

這一切!

相關問題