Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
795 views
in Technique[技术] by (71.8m points)

iphone - UITableView doesn't keep row selected upon return

i have several table views in my app. I am familiar with the usual behaviour of tables, that when you select a row and progress to a pushed view, it turnes blue, then when you go back it stays blue for a split second then fades to white, in order to notify the user of what row was just selected.

This worked perfectly up until recently when i noticed it no longer did the last bit of what I described: it didn't stay blue for that split second...

I have no idea why, but after reading a few related posts on this site i realised that the piece of code that does that gets called is in the "viewDidAppear" method. What confuses me is that I don't override this method, but i did test it with an NSLog to show me the index path for the row it should deselect, which returned (null).

So this is leading me to believe that somehow, the tableView is prematurely deselecting the row.

Below is my didSelectRowForIndexPath method:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Selected row at index path: %@", indexPath);

    //setting our view controller as what we want
    TripDetails *detailViewController = [[TripDetails alloc] initWithNibName:@"TripDetails" bundle:nil];
    self.tripsDetailViewController = detailViewController;
    [detailViewController release];


 // Pass the selected object to the new view controller.
    NSLog(@"Passing trip...");
    self.tripsDetailViewController.selectedTrip =  [fetchedResultsController objectAtIndexPath:indexPath];

    //Hide the bottom bar on pushed view
    tripsDetailViewController.hidesBottomBarWhenPushed = YES;

   [self.navigationController pushViewController:tripsDetailViewController animated:YES];

}

Any help is greatly appreciated, thanks :)


EDIT: Fixed

I got it working.... Seems i was calling the [self.tableView reloadData] method in my viewWillAppear method which then caused the table to deselect all cells. Below is my modified viewDidLoad method. Thanks for the suggestions!

- (void)viewWillAppear:(BOOL)animated 
{
NSLog(@"running viewWIllAppear for TripsTable");

//Getting indexpath for highlighened cell, to rehighlight
NSIndexPath *selectedIndex = [self.tableView indexPathForSelectedRow];

//Refreshing Table - Implement an if statement on the condition that the data has     changed
[self viewDidLoad];
[self.tableView reloadData];

//Re select cell
[self.tableView selectRowAtIndexPath:selectedIndex animated:NO scrollPosition:UITableViewScrollPositionNone];


[super viewWillAppear:animated];
 }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Its a feature you can turn off.

In the UITableViewController you can call

[ self setClearsSelectionOnViewWillAppear:NO ];

This is directly from the file "UITableViewController.h". This feature is documented there.

   @property(nonatomic) BOOL clearsSelectionOnViewWillAppear __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_3_2); 
// defaults to YES. If YES, any selection is cleared in viewWillAppear:

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...