Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell doesn't get deselected when swiping back quickly

I've now updated three of my apps to iOS 7, but in all three, despite them not sharing any code, I have the problem where if the user swipes to go back in the navigation controller (rather than tap the back button) quickly, the cell will remain in its selected state.

For the three apps, one uses custom cells created programmatically, another uses custom cells created in a storyboard and the third uses default cells in a very basic subclass of UITableView, also in a storyboard. In all three cases, the cells don't deselect by themselves. If the user swipes slowly, or hits the back button, they deselect as normal.

This is only happening in my iOS 7 apps, Apple's own apps and third party apps upgraded for iOS 7 all seem to be behaving normally (albeit with slight differences in how quickly the cells gets deselected).

There must be something I'm doing wrong, but I'm not sure what?

like image 923
Robert Avatar asked Oct 15 '13 10:10

Robert


People also ask

What is a uitableviewcell in Salesforce?

A UITableViewCell object is a specialized type of view that manages the content of a single table row. You use cells primarily to organize and present your app’s custom content, but UITableViewCell provides some specific customizations to support table-related behaviors, including: Applying a selection or highlight color to the cell.

How to get the index path of a UITableView cell?

The trick is to get the superview of the button, which will be the cell, and then using tableView.indexPathForCell (cell) to get the index path. @IBAction func tapOnButton(sender: UIButton) { let cell = sender.superview as! UITableViewCell let indexPath = tableView.indexPathForCell(cell) }

How do you display content in uitableviewcell?

For the standard types, UITableViewCell provides the views for displaying your content. All you have to do is assign values to the textLabel, detailTextLabel, and imageView properties of your cell. The following illustration shows how the values you supply are positioned within the cell’s content area.

How to add a uigesturerecognizer to a uitableviewcell?

But the best answer is to create your custom UITableViewCell, then have your UIButton delegate the call to your view controller. This requires more code, and a custom class, but is the most correct. If you are tapping on any other UIView, you could handle similarly. But in this case, you cannot add the UIGestureRecognizer to the view on storyboard.


2 Answers

This worked best for me:

- (void)viewDidLoad {     [super viewDidLoad];     self.clearsSelectionOnViewWillAppear = NO; }  -(void)viewWillAppear:(BOOL)animated {     [super viewWillAppear:animated];     [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:animated]; } 

I even got a much better unselect fading while I was swiping back slowly.

like image 143
emreberge Avatar answered Sep 27 '22 22:09

emreberge


I'm dealing with the same problem right now. The UICatalog-sample from Apple seems to bring the dirty solution.

It really doesn't make me happy at all. As mentioned before it uses [self.tableView deselectRowAtIndexPath:tableSelection animated:NO]; to deselect the currently selected row.

- (void)viewWillAppear:(BOOL)animated {     [super viewWillAppear:animated];      // this UIViewController is about to re-appear, make sure we remove the current selection in our table view     NSIndexPath *tableSelection = [self.tableView indexPathForSelectedRow];     [self.tableView deselectRowAtIndexPath:tableSelection animated:NO];      // some over view controller could have changed our nav bar tint color, so reset it here     self.navigationController.navigationBar.tintColor = [UIColor darkGrayColor]; } 

I have to mention the sample code may not be iOS 7 iOS 8 iOS 9 iOS 10-ready


Something which really confuses me is the UITableViewController Class Reference:

When the table view is about to appear the first time it’s loaded, the table-view controller reloads the table view’s data. It also clears its selection (with or without animation, depending on the request) every time the table view is displayed. The UITableViewController class implements this in the superclass method viewWillAppear:. You can disable this behavior by changing the value in the clearsSelectionOnViewWillAppear property.

This is exactly the behavior I expect… but it does not seem to work. Neither for you nor for me. We really have to use the "dirty" solution and do it on our own.

like image 45
Fabio Poloni Avatar answered Sep 27 '22 22:09

Fabio Poloni