IPhone UITableView cells remain selected

in my UITableView sometimes the cells remain selected after touching. Since this happens only occasionally, I cannot reproduce the problem.

Any clues? Maybe this has something to do with the inefficient release of tableView?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSUInteger row = [indexPath row]; [tableView deselectRowAtIndexPath:indexPath animated:YES]; switch (row) { case 0: FruitViewController *fruitController = [FruitViewController alloc]; [fruitController retain]; [fruitController initWithNibName:@"FruitView" bundle:[NSBundle mainBundle]]; [self.navigationController pushViewController:fruitController animated:YES]; [fruitController release]; break; case 1: CerealsViewController *cerealsController = [CerealsViewController alloc]; [cerealsController retain]; [cerealsController initWithNibName:@"CerealsView" bundle:[NSBundle mainBundle]]; [self.navigationController pushViewController:cerealsController animated:YES]; [cerealsController release]; break; default: break; } } 
+45
iphone cocoa-touch uitableview
May 22 '09 at 9:32 a.m.
source share
7 answers

I can’t tell you why you see the problem, but here are some tips to fix it:

According to the Apple HIG, the selection should not disappear until it returns from the view controller, just pushed onto the stack. If your controller is only a UITableViewController, it should automatically deselect after returning to the view. If not, add

 - (void) viewWillAppear:(BOOL)animated { [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:animated]; [super viewWillAppear:animated]; } 

somewhere in the view controller.

If there are any lines that, when clicked, do not go into another view and actually do nothing when selected, they should not be selected, so you can override this in

 - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath 

and return nil in cases where the row should not be selected.

+98
May 22, '09 at 13:15
source share

One possible reason is to override -viewWillAppear:animated: without calling [super viewWillAppear] in the controller, which extends the UITableViewController . Adding [super viewWillAppear:animated] at the beginning of your -viewWillAppear method may fix the problem.

+21
Jul 16 '11 at 1:13
source share

To increase the response of "Ed Marty", I decided to add a cancellation in the "viewWillDisappear" method because I thought it looked better.

In addition to this, I used a UITableView with a regular UIViewController (and not a UITableViewController), so the tableView variable was not available to me. To overcome this, I added the tableView property to my view manager header file (and in my XIB file I connected the actual table view to the property) ...

 @property (nonatomic,retain) IBOutlet UITableView *tableView 

... and in the implementation of the view controllers, they connected the property and performed deselection as follows.

 @synthesize tableView - (void) viewWillDisappear:(BOOL)animated { [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:animated]; [super viewWillDisappear:animated]; } 
+11
May 6 '11 at 7:53 a.m.
source share

If you are using a UITableViewController, you can use:

 self.clearsSelectionOnViewWillAppear = YES; 
+8
Apr 3 '13 at 20:02
source share

In my case, I will remove the cell right at the end of didSelectRowAtIndexPath:

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Do smth here. segue ..etc [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO]; } 
+4
Sep 06 '13 at 7:55
source share

Use this method in the UITableViewCell class

 (void)setSelected:(BOOL)selected animated:(BOOL)animated { // Just comment This line of code // [super setSelected:selected animated:animated]; } 

or even you can try this code in didSelectRowAtIndexPath method

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

// Do some things when choosing a row

 [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

}

0
Apr 14 '15 at 8:02
source share

Solving this takes about two seconds, once you know how tables are managed.

As soon as the cells scroll from the screen, they lose their formatting. The table minimizes memory usage by rejecting other cells and re-creating them if / when the user scrolls back.

Even things like reloading tabular data really only apply to cells on the screen at that moment. The point is that cellForRowAtIndexPath can create the same cell five times, without the user ever having to click a button or leave a view controller! Therefore, configure it to reload any formatting. Add something like this to cellForRowAtIndexPath:

  if arrayOf_SelectedCells.contains(indexPath) { setup_Selected(cell) //Where setup_Selected is a custom function to apply formatt } else { setup_Unselected(cell) } 
0
Feb 24 '16 at 15:20
source share



All Articles