UITableView 在返回时不保持选中行

UITableView doesn#39;t keep row selected upon return(UITableView 在返回时不保持选中行)

本文介绍了UITableView 在返回时不保持选中行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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...

我不知道为什么,但是在阅读了该站点上的一些相关帖子后,我意识到执行该操作的代码段位于viewDidAppear"方法中.让我困惑的是我没有覆盖这个方法,但我确实使用 NSLog 对其进行了测试,以向我显示它应该取消选择的行的索引路径,它返回 (null).

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).

所以这让我相信,不知何故,tableView 过早地取消选择该行.

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

下面是我的 didSelectRowForIndexPath 方法:

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 :)

已修复

我让它工作了......似乎我在我的 viewWillAppear 方法中调用了 [self.tableView reloadData] 方法,然后导致表格取消选择所有单元格.下面是我修改后的 viewDidLoad 方法.感谢您的建议!

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];
 }

推荐答案

这是一个可以关闭的功能.

Its a feature you can turn off.

在 UITableViewController 中可以调用

In the UITableViewController you can call

[ self setClearsSelectionOnViewWillAppear:NO ];

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

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:

这篇关于UITableView 在返回时不保持选中行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:UITableView 在返回时不保持选中行

基础教程推荐