开发者

Keep the UITableView selection in sync with the active detail view item

开发者 https://www.devze.com 2023-04-05 21:05 出处:网络
I have a situation very much like in Apple\'s mail.app on the iPhone where a user selects an item from a list (UITableView) to be displayed in a detail view. The user might navigate between items in t

I have a situation very much like in Apple's mail.app on the iPhone where a user selects an item from a list (UITableView) to be displayed in a detail view. The user might navigate between items in this view and then return back to the list.

Mail.app modifies the selection on the list and stays in sync with the detail view and when the user returns back to the list, the detail view's item is correctly highlighted instead of the initially selected. Mail.app even scrolls to display the righ item respecting the direction of navigation so that going messages upwards (the up arrow) makes the highlighted list item appear at the top of the screen and vice versa.

Now, my question is that what would be the best way to get thi开发者_如何学Gos kind of behaviour? I do know how to keep the list in sync with the detail view. What I quite don't understand is how to tell if the list needs to be scrolled up or down for the new selection and what is the right way to flash the highlighted item. I don't want the list to scroll if not necessary. I can calculate how many items the user has advanced but I have no idea if the new item will be visible on the screen.

The highlight I have currently implemented so that table view selects an item without animation and then deselects the same item with animation in viewWillAppear:.

Any pointers on this would be highly appreciated.


You can ask the table view if a cell is visible, for example by sending it a cellForRowAtIndexPath: message and checking the return value against nil.

To flash the item like Mail.app, it seems you are already doing the right thing.


You could use

-(void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition (UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

or

-(void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

in your viewWillAppear: in order to scroll your tableview to the right cell.

Then, the "flashing" of the item should be something like

-(void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;


I assume you're in a UINavigationController and your "detail" view gets pushed on top when selecting something from your UITableView. When returning from your detail view, the UITableViewController's viewWillAppear: will be triggered. In there you should use selectRowAtIndexPath:animated:scrollPosition: (see Apple's documentation here http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITableView_Class/Reference/Reference.html) to scroll the selected item into view and define where to show it using the "scrollPosition" parameter.

To know which item was selected when navigating in your detail view, you could hold a member variable in your app delegate (simple, not very elegant, but works).

0

精彩评论

暂无评论...
验证码 换一张
取 消