我在我的应用程序中有几个表视图。我熟悉表格的常见行为,当您选择一行并进入推送的视图时,它会变成蓝色,然后当您返回时,它会一秒钟保持蓝色,然后变为白色,以便通知用户刚刚选择了哪一行。UITableView在返回时不会保留行选择
这完美地工作,直到最近,当我发现它不再做什么,我描述的最后一位:它没有停留蓝色那一刹那......
我不知道为什么,但经过阅读本网站上的一些相关帖子,我意识到那个被调用的代码片段在“viewDidAppear”方法中。令我困惑的是,我没有重写这个方法,但是我用NSLog测试了它,向我显示了它应该取消选择的行的索引路径,它返回(null)。
所以这让我相信,不知何故,tableView过早取消选择该行。
下面是我的didSelectRowForIndexPath方法:
- (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];
}
任何帮助是极大的赞赏,感谢:)
编辑:固定
我得到它的工作....看来我在我的viewWillAppear方法中调用[self.tableView reloadData]方法,然后导致表取消选择所有单元格。以下是我修改后的viewDidLoad方法。感谢您的建议!
- (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];
}
瞎猜作品:也许以前的视图控制器,因为内存使用的卸载。这可能会导致这种行为。 – dasdom