Removing UITableViewCell Selection

19,029

Solution 1

You could try:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

if you just want the highlight to go away after selecting a cell. Unless I misunderstand your question.

Solution 2

You can also try this

tableView.allowsSelection = NO;

another way of doing this

cell.selectionStyle = UITableViewCellSelectionStyleNone;

one more

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

Solution 3

You can also do it from the storyboard. Select the tableViewCell and under Attributes Inspector you can choose Selection > None

enter image description here

Solution 4

Here is a Swift 2 version

    if let indexPaths = self.tableView.indexPathsForSelectedRows {
        for indexPath in indexPaths {
            self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
        }
    }

Solution 5

Unselect selected row(s). You do not even need to know which one it is.

tableView.selectRow(at: nil, animated: true, scrollPosition: .none)
Share:
19,029

Related videos on Youtube

Apollo
Author by

Apollo

Updated on June 04, 2022

Comments

  • Apollo
    Apollo almost 2 years

    Currently I'm overriding the standard UITableViewSelectionStyle by using UITableViewSelectionStyleNone and then changing the color the cell based on delegate methods:

    - (void)tableView:(UITableView *)tableView 
          didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];                
        [cell setBackgroundColor:[UIColor yellowColor]];
    }
    
    - (void)tableView:(UITableView *)tableView 
        didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        [cell setBackgroundColor:[UIColor whiteColor]];
    }
    
    - (void)tableView:(UITableView *)tableView 
        didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath {
        NSLog(@"indexpath: %i",indexPath.row);
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        [cell setBackgroundColor:[UIColor whiteColor]];
    }
    
    - (void)tableView:(UITableView *)tableView 
        didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        [cell setBackgroundColor:[UIColor whiteColor]];
    }
    

    This almost works except that whenever I highlight a cell and then drag my finger off of it without actually selecting it, the color doesn't change to white...if I set it to [UIColor RedColor] it works perfeclty. Why is this...

    Edit:

    Somehow when I print out the indexPath.row after didUnhlightRowAtIndexPath I get "indexpath: 2147483647" from my NSLog

    • Malloc
      Malloc over 10 years
      indexPath 2147483647 is equivalent to NSNotFound.
  • Apollo
    Apollo over 10 years
    not sure why I got a downvote...just explaining how I solved my own problem for the benefit of others on Stack...
  • abbood
    abbood over 10 years
    Yeah I feel your pain and hate it when that happens.. +1 for both your question and answer my friend :)
  • sosborn
    sosborn over 10 years
    You don't have to maintain the state. UITableView has the method indexPathsForSelectedRows