Подтвердить что ты не робот

Цвет элемента UITableView textLabel

У меня простая проблема с 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;
}

Он работает, но при прокрутке таблицы просмотра цвет меняется обратно.

4b9b3361

Ответ 1

Получено решение путем установки цвета в 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];

}

Спасибо

Ответ 2

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

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

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

который будет работать нормально.

Ответ 3

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

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

и

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

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

в

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

что все!