Automatic deselection of table view cell after popping detail view?

I've been stumped by this for a while...

When you tap the cell of a contact in Apple's Phone app for the iPhone (entering the view with contact details) and tap back to the previous view, there is a brief animation (fading from blue to white) showing the deselection of the cell. This is the behavior with all table views in Apple's own apps, and also the recommended behavior according to their Human Interface Guidelines.

However, in my own project I've been having trouble replicating this behavior. None of the cells in my UITableView are selected when I return to it from a detail view.

I looked through the CoreDataBooks sample code in Apple's documentation, which has the desired cell deselection behavior, and it seems like the table view gets the behavior "automatically" (without any specific implementation).

I've also tried implementing the solutions in these very similar questions:

What deselect selected cell when navigationController pops a view?

iPhone UITableView cells stay selected

UITableView doesn't keep row selected upon return

But I always get the same result -- none of the cells in the UITableView are selected when I return to it. (Even after adding [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:animated]; in the view controller's -viewWillAppear: method.)

It was also suggested in the comments of one question that having [[self tableView] reloadData]; in -viewWillAppear: may be causing the cells not to stay selected. But CoreDataBooks does the same thing and still has the desired behavior (seemingly) without any specific code.

Any suggestions on how to resolve the problem? Thanks in advance.

On a side note, I don't quite understand why the code to deselect the cell should be implemented in -viewWillAppear: (rather than -viewDidAppear: ). Wouldn't that cause everything to happen before the table view is displayed on screen? This is probably just due to lack of proper understanding of a view's life cycle on my part, but any clarifications would be nice. Thanks again.


Have you experimented with the clearsSelectionOnViewWillAppear property of a UITableViewController (not just a tableview but a tableviewcontroller)? If you set it to NO it will make the selection stay blue.
To get your desired effect,you will also probably need to add a call to
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated
to ensure that it doesn't stay blue forever.

Look in the Overview section of the UITableView Documentation to get started.

//EDIT// As noted in the comments below, what we finally ended up doing is moving the call to [[self tableview] reloadData]; to viewDidAppear .
My guess is that CoreDataBooks has a complex enough table (with sections and books and sections and books) that something is going on with the timing of events.


If you want to deselect a cell, use the - (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated and use [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; or [tableView deselectRowAtIndexPath:indexPath animated:YES]; .

链接地址: http://www.djcxy.com/p/10302.html

上一篇: 没有显示附件

下一篇: 弹出详细视图后自动取消选择表视图单元格?