2012-03-16 169 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 

这一切!

相关问题