UITableView在返回时不会保留行

Son*_*pop 9 iphone objective-c uitableview ios4

我的应用程序中有几个表视图.我熟悉表格的通常行为,当你选择一行并进入推动视图时,它会变为蓝色,然后当你返回时它会保持蓝色一瞬间然后渐变为白色,以便通知用户刚刚选择了哪一行

直到最近,当我注意到它不再是我所描述的最后一点时,它完美地工作了:它在那一瞬间没有保持蓝色......

我不明白为什么,但在阅读了这个网站上的一些相关帖子后,我意识到调用它的代码片段是在"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];

}
Run Code Online (Sandbox Code Playgroud)

非常感谢任何帮助,谢谢:)


编辑:修正

我得到了它....似乎我在我的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];
 }
Run Code Online (Sandbox Code Playgroud)

jan*_*oen 29

它是一个你可以关闭的功能.

在UITableViewController中,您可以调用

[ self setClearsSelectionOnViewWillAppear:NO ];
Run Code Online (Sandbox Code Playgroud)

这直接来自文件"UITableViewController.h".此功能记录在那里.

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

  • 请注意,从Xcode 6.3/iOS 8.3开始,在IB/storyboards/XIB中设置时,此设置不会保留.您可以选中该框,但未正确初始化; 它始终设置为true. (2认同)

Pra*_*n S 9

尝试

[tableView deselectRowAtIndexPath:indexPath animated:NO];

取消选择一行和

[tableView selectRowAtIndexPath:indexPath animated:NO];
Run Code Online (Sandbox Code Playgroud)

用于突出显示所选行.