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
Related videos on Youtube
Author by
Ram
Updated on March 09, 2020Comments
-
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 over 11 yearsThis question need to have an accepted answer
-
-
Ram over 13 yearsi 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 over 11 yearsIt's worth noting that
indexPathForSelectedRow
is a method and not a property. -
Hugo Dozois about 11 yearstry explaining your answer, else it should be a comment.
-
El Tomato about 11 yearsGood job... I think 'index' on the 2nd line should be indexPath
-
abc123 about 11 yearsSweet! Didn't know about
[myTable indexPathForSelectedRow]
. I was looping through the cells before. Thanks! -
devios1 almost 11 years@FreeAsInBeer Is this distinction important somehow? Property accessors are methods.
-
FreeAsInBeer almost 11 years@chaiguy That's fine if you don't mind your colleagues considering you evil.
-
huggie about 10 yearsThere is a didDeselectRowAtIndexPath to use to deselect the previous one.
-
Milo almost 10 yearsI like to think I can't upvote this post because it would cause an integer overflow :)
-
Damir Kotoric over 9 yearsWas calling the wrong method this whole time, arghh!! Thank you!
-
spassas over 9 yearsAbsolutely correct. I think that
[cell setSelected:YES]
could be omitted:selectRowAtIndexPath
will take care of the cell'sselected
state -
Michael over 8 yearsDoesn't need to be declared non-optional but this is correct.
-
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 over 8 years@Supertecnoboff There is no difference in performance between the two variations.
-
Fattie about 8 yearshi @ram, you need to select an answer on this question
-
George Asda over 7 yearsoh man.... bloody autocompletion!!! It took me several hours to figure it out!!! OMG...I want to kiss you!
-
C. Tewalt over 7 yearsI don't understand... this code is never going to let anything get selected.
-
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 about 7 yearsIn 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 almost 7 yearsthe perfect one as i needed
-
elysch over 6 yearsThis worked for me in Swift 3. Just had to add
self.tableView(tableView, didDeselectRowAt: indexPath)
aftertableView.deselectRow...
because it was not called automatically -
Boris Nikolic over 6 yearsthis is not supported on iOS 11 Swift 4
-
Mehdi Chennoufi over 5 yearsuntil 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 over 5 yearsThis answer works for UITableView's with multiple selections. Thumbs up.
-
Fattie almost 5 yearsThis is certainly the correct approach, when you're not editing, when you're "tapping a row to do something"