2016-06-28 8 views
2

Я хочу, чтобы цвет текста моей ячейки менялся при использовании, но не цвет фона.Цвет фона UITableViewCell при подсвечивании/выборе

Я хочу, чтобы фон ячейки всегда был белым, и только цвет текста изменился при выборе.

Я видел кучу ответов на вопросы о том, как это сделать ...

UIView *bgColorView = [[UIView alloc] init]; 
bgColorView.backgroundColor = [UIColor whiteColor]; 
[cell setSelectedBackgroundView:bgColorView]; 

... но вид, что получает созданные работает на верхней части сепаратора клеток.

enter image description here

И использовать cell.textLabel.highlightedTextColor = [UIColor brownColor]; изменить TextColor, я не могу иметь cell.selectionStyle = UITableViewCellSelectionStyleNone;, так что мне нужно, чтобы выяснить что-то.

ответ

3

, если вы хотите, чтобы показать разделитель ячейки, вам может понадобиться это:

добавить этот код:

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

cell.selectionStyle = UITableViewCellSelectionStyleDefault; 
cell.textLabel.highlightedTextColor = [UIColor brownColor]; 

UIView *backgroudView = [[UIView alloc]initWithFrame:CGRectMake(0, 1, tableView.bounds.size.width, [self tableView:tableView heightForRowAtIndexPath:indexPath] - 2)]; 
backgroudView.backgroundColor = [UIColor whiteColor]; 

UIView *placeholderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)]; 
placeholderView.backgroundColor = [UIColor clearColor]; 
[placeholderView addSubview:backgroudView]; 
cell.selectedBackgroundView = placeholderView; 

... 
} 
// These codes are used to show the separatorView when the cell didSelected 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    void (^showSeparatorView)(UITableViewCell *cell) = ^(UITableViewCell *cell){ 
     for (id obj in cell.subviews) { 
      if([obj isKindOfClass:NSClassFromString(@"_UITableViewCellSeparatorView")]){ 
       UIView *view = (UIView *)obj; 
       view.hidden = NO; 
      } 
     } 
    }; 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    showSeparatorView(cell); 
    if (indexPath.row > 0) 
    { 
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row - 1 inSection:indexPath.section]]; 
     showSeparatorView(cell); 
    } 
... 
} 

// These codes are used to show the separatorView when the cell didHightlight 
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    void (^showSeparatorView)(UITableViewCell *cell) = ^(UITableViewCell *cell){ 
     for (id obj in cell.subviews) { 
      if([obj isKindOfClass:NSClassFromString(@"_UITableViewCellSeparatorView")]){ 
       UIView *view = (UIView *)obj; 
       view.hidden = NO; 
      } 
     } 
    }; 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    showSeparatorView(cell); 
    if (indexPath.row > 0) 
    { 
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row - 1 inSection:indexPath.section]]; 
     showSeparatorView(cell); 
    } 
} 

Надежда это может помочь вам

0

Добавьте это в методе cellForRowAtIndexPath так, когда вы нажмете в ячейке, чем цвет текста изменение этикетки

cell.textLabel.highlightedTextColor = [UIColor brownColor]; 
+0

Да, у меня есть что, но как насчет части, где разделитель не отображается на ячейке, которая прослушивается? – SRMR

0

Вы можете использовать это -

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

    UITableViewCell *c = [tableView cellForRowAtIndexPath:indexPath]; 
    [c.textLabel setTextColor:[UIColor brownColor]]; 
} 
0

Вы можете добавить код cell.selectedBackgroundView = [UIView new];, чтобы выяснить это. С помощью этого метода вам не нужен cell.selectionStyle = UITableViewCellSelectionStyleNone;.

Смежные вопросы