How to deselect a selected UITableView cell?

207,683

Solution 1

use this code

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
 {
    //Change the selected background view of the cell.
     [tableView deselectRowAtIndexPath:indexPath animated:YES];
 }

Swift 3.0:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    //Change the selected background view of the cell.
    tableView.deselectRow(at: indexPath, animated: true)
}

Solution 2

Use the following method in the table view delegate's didSelectRowAtIndexpath (Or from anywhere)

[myTable deselectRowAtIndexPath:[myTable indexPathForSelectedRow] animated:YES];

Solution 3

It might be useful to make an extension in Swift for this.

Swift 4 and Swift 5:

Swift extension (e.g. in a UITableViewExtension.swift file):

import UIKit

extension UITableView {

    func deselectSelectedRow(animated: Bool)
    {
        if let indexPathForSelectedRow = self.indexPathForSelectedRow {
            self.deselectRow(at: indexPathForSelectedRow, animated: animated)
        }
    }

}

Use e.g.:

override func viewWillAppear(_ animated: Bool)
{
    super.viewWillAppear(animated)

    self.tableView.deselectSelectedRow(animated: true)
}

Solution 4

Try this:

for (NSIndexPath *indexPath in tableView.indexPathsForSelectedRows) {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

Solution 5

Please check with the delegate method whether it is correct or not. For example;

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

for

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Share:
207,683

Related videos on Youtube

Ram
Author by

Ram

Updated on March 09, 2020

Comments

  • Ram
    Ram about 4 years

    I am working on a project on which I have to preselect a particular cell.

    I can preselect a cell using -willDisplayCell, but I can't deselect it when the user clicks on any other cell.

    - (void)tableView:(UITableView*)tableView 
            willDisplayCell:(UITableViewCell*)cell
            forRowAtIndexPath:(NSIndexPath*)indexPath
    { 
        AppDelegate_iPad *appDelegte = 
          (AppDelegate_iPad *)[[UIApplication sharedApplication] delegate];
    
        if ([appDelegte.indexPathDelegate row] == [indexPath row])
        {
            [cell setSelected:YES];    
        } 
    }
    
    - (void)tableView:(UITableView *)tableView 
            didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
        AppDelegate_iPad *appDelegte = 
          (AppDelegate_iPad *)[[UIApplication sharedApplication] delegate];
    
        NSIndexPath *indexpath1 = appDelegte.indexPathDelegate;
        appDelegte.indexPathDelegate = indexPath;
        [materialTable deselectRowAtIndexPath:indexpath1 animated:NO];
    }
    

    Can you help?

    • Titouan de Bailleul
      Titouan de Bailleul over 11 years
      This question need to have an accepted answer
  • Ram
    Ram over 13 years
    i have tested with NO but that also not solve my problem. but i fixed this issue with another way as reloaded Data when ever the didselect delegate fired.
  • FreeAsInBeer
    FreeAsInBeer over 11 years
    It's worth noting that indexPathForSelectedRow is a method and not a property.
  • Hugo Dozois
    Hugo Dozois about 11 years
    try explaining your answer, else it should be a comment.
  • El Tomato
    El Tomato about 11 years
    Good job... I think 'index' on the 2nd line should be indexPath
  • abc123
    abc123 about 11 years
    Sweet! Didn't know about [myTable indexPathForSelectedRow]. I was looping through the cells before. Thanks!
  • devios1
    devios1 almost 11 years
    @FreeAsInBeer Is this distinction important somehow? Property accessors are methods.
  • FreeAsInBeer
    FreeAsInBeer almost 11 years
    @chaiguy That's fine if you don't mind your colleagues considering you evil.
  • huggie
    huggie about 10 years
    There is a didDeselectRowAtIndexPath to use to deselect the previous one.
  • Milo
    Milo almost 10 years
    I like to think I can't upvote this post because it would cause an integer overflow :)
  • Damir Kotoric
    Damir Kotoric over 9 years
    Was calling the wrong method this whole time, arghh!! Thank you!
  • spassas
    spassas over 9 years
    Absolutely correct. I think that [cell setSelected:YES] could be omitted: selectRowAtIndexPath will take care of the cell's selected state
  • Michael
    Michael over 8 years
    Doesn't need to be declared non-optional but this is correct.
  • Supertecnoboff
    Supertecnoboff over 8 years
    @FreeAsInBeer But even if it is a method call, all its doing is retrieving one value - not going to affect performance right? Even on the slowest of devices.
  • FreeAsInBeer
    FreeAsInBeer over 8 years
    @Supertecnoboff There is no difference in performance between the two variations.
  • Fattie
    Fattie about 8 years
    hi @ram, you need to select an answer on this question
  • George Asda
    George Asda over 7 years
    oh man.... bloody autocompletion!!! It took me several hours to figure it out!!! OMG...I want to kiss you!
  • C. Tewalt
    C. Tewalt over 7 years
    I don't understand... this code is never going to let anything get selected.
  • HuaTham
    HuaTham over 7 years
    @matrixugly is right. It's more appropriate to do this inside willSelectRowAt:. See a more complete answer here: stackoverflow.com/questions/12693402/…
  • MonsieurDart
    MonsieurDart about 7 years
    In my opinion, this answer is better because putting the desolation in the viewWillAppear() method helps the user to understand where he comes back from.
  • Patel Jigar
    Patel Jigar almost 7 years
    the perfect one as i needed
  • elysch
    elysch over 6 years
    This worked for me in Swift 3. Just had to add self.tableView(tableView, didDeselectRowAt: indexPath) after tableView.deselectRow... because it was not called automatically
  • Boris Nikolic
    Boris Nikolic over 6 years
    this is not supported on iOS 11 Swift 4
  • Mehdi Chennoufi
    Mehdi Chennoufi over 5 years
    until you add selection on editing in your tableview, it will be impossible to select any cell because it will deselect it when you done selecting it :)
  • Danny Bravo
    Danny Bravo over 5 years
    This answer works for UITableView's with multiple selections. Thumbs up.
  • Fattie
    Fattie almost 5 years
    This is certainly the correct approach, when you're not editing, when you're "tapping a row to do something"