2

I want my cell text color to change when tapped, but not the background color.

I want the cell background to always be white, and only the text color to change when selected.

I've seen a bunch of answers about how to do this...

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

... but the view that gets created runs on top of the cell separator.

enter image description here

And to use cell.textLabel.highlightedTextColor = [UIColor brownColor]; to change the textColor, I can't have cell.selectionStyle = UITableViewCellSelectionStyleNone;, so I need to figure out something.

SRMR
  • 3,064
  • 6
  • 31
  • 59

4 Answers4

3

if you want to show the separator of cell , you may need this:

add this code :

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

Hope these can help to you!

DC.Static
  • 56
  • 5
  • This is *really* expensive, and uses a private API (`_UITableViewCellSeparatorView`). We abandoned this approach after a year--use with caution. – Stan Aug 19 '18 at 22:25
0

Add this in cellForRowAtIndexPath method so when you tap in cell than change label text color

cell.textLabel.highlightedTextColor = [UIColor brownColor];
Jigar
  • 1,801
  • 1
  • 14
  • 29
  • Yup, I've got that, but what about the part where the separator is not shown on the cell that is tapped? – SRMR Jun 28 '16 at 13:32
0

You can use this -

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

    UITableViewCell *c = [tableView cellForRowAtIndexPath:indexPath]; 
    [c.textLabel setTextColor:[UIColor brownColor]];
}
Abhishek Mishra
  • 1,625
  • 16
  • 32
0

You can add the code cell.selectedBackgroundView = [UIView new]; to figure out this. With this method, you don't need cell.selectionStyle = UITableViewCellSelectionStyleNone;.

DC.Static
  • 56
  • 5