UITableView ячейка textLabel цвет - PullRequest
14 голосов
/ 16 марта 2012

У меня есть простая проблема с UITableViewCell. То, что я хочу, это изменить цвет текста выбранной ячейки. В моем методе cellForRowAtIndexPath я установил:

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

Если selectionStyle равно UITableViewCellSelectionStyleNone, highlightedTextColor не изменится. Поэтому я использую эти два метода для установки цвета текста:

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

Работает, но при прокрутке табличного вида цвет меняется обратно.

Ответы [ 3 ]

20 голосов
/ 16 марта 2012

Получил решение, установив цвет в проверке if (cell == nil)

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 голосов
/ 16 марта 2012

Все, что вам нужно сделать, это

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

внутри if (cell == nil)

, которая будет работать нормально.

0 голосов
/ 16 марта 2012

установить глобальный: int m_selectedCell; изменить его в

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

и

просто добавить

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

в

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

это все!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...