2011-07-11 17 views
9

我在我的应用程序中有几个表视图。我熟悉表格的常见行为,当您选择一行并进入推送的视图时,它会变成蓝色,然后当您返回时,它会一秒钟保持蓝色,然后变为白色,以便通知用户刚刚选择了哪一行。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]; 
} 
+1

瞎猜作品:也许以前的视图控制器,因为内存使用的卸载。这可能会导致这种行为。 – dasdom

回答

9

尝试

[tableView deselectRowAtIndexPath:indexPath animated:NO];

用于取消的行和

[tableView selectRowAtIndexPath:indexPath animated:NO]; 

用于突出显示所选择的行。

1

使用此片的代码在表视图的didSelectRowAtIndexPath方法委托方法结束时,

[tableView deselectRowAtIndexPath:indexPath animated:NO]; 
2

类的UITableView显然viewWillAppear:并取消选择所有细胞,即使不重新加载细胞。诀窍描述为我工作:从我UITablViewController

代码:

- (void)viewWillAppear:(BOOL)animated 
{ 
    NSIndexPath *selectedIndex = [self.tableView indexPathForSelectedRow]; 

    [super viewWillAppear:animated]; 

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

这就是我怀疑的事情......请注意:不要再使用UITableViewController! :p – Giovanni

+1

这是不正确的,应该收回。它由clearsSelectionOnViewWillAppear控制,请参阅上面的@ jankoen的回答(和我的评论) –

29

它的一个功能,您可以关闭。

在的UITableViewController您可以拨打

[ self setClearsSelectionOnViewWillAppear:NO ]; 

这是直接从文件 “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: 
+0

谢谢,这对我来说! – Mike

+0

我试图在'UITableView'头中找到这个属性:/ – rounak

+2

请注意,从Xcode 6.3/iOS 8.3开始,在IB/storyboards/XIBs中设置时,此设置不会保留。您可以选中该框,但未正确初始化;它始终设置为true。 –

0
NSIndexPath *_selectedIndex ; 

使用上面的全局变量在表视图委托方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
_selectedIndex = [self.tableView indexPathForSelectedRow]; 
} 

UINavigationController委托方法的下列代表

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated 
{ 
[self.tableView selectRowAtIndexPath:_selectedIndex animated:NO scrollPosition:UITableViewScrollPositionNone]; 

} 
0

我所选择存储不知道这是否相关,但UITableViewController中有一个标志,我只是没有叫做

@property(nonatomic) BOOL clearsSelectionOnViewWillAppear NS_AVAILABLE_IOS(3_2); // defaults to YES. If YES, any selection is cleared in viewWillAppear: 

我认为这应该解决一些人们在这里描述的问题。

0

下面的代码确实好

-(void)viewWillAppear:(BOOL)animated{ 
     [super viewWillAppear:YES]; 
     [self setClearsSelectionOnViewWillAppear:NO ]; 
    } 
相关问题