Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone UITableView cells stay selected

in my UITableView sometimes cells stay selected after touching. Because it happens only occasionally, I'm not able to reproduce the problem.

Any hints? Maybe it has something to do with unproper releasing 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;    }    } 
like image 218
Stefan Avatar asked May 22 '09 09:05

Stefan


2 Answers

I can't tell you why you're seeing the issue, but here are some suggestions for fixing it:

According to the Apple HIG, the selection should not disappear until returning from the view controller just pushed onto the stack. If your controller is just a UITableViewController, it should deselect automatically upon 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 rows that, when clicked, do not go to another view, and don't in fact do anything when selected, they shouldn't be selectable, so you can override that in

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

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

like image 161
Ed Marty Avatar answered Oct 04 '22 04:10

Ed Marty


One possible cause is overriding -viewWillAppear:animated: without calling [super viewWillAppear] in a controller that extends UITableViewController. Adding [super viewWillAppear:animated] at the beginning of your -viewWillAppear method may rectify the problem.

like image 29
Fiid Avatar answered Oct 04 '22 05:10

Fiid