您的位置:首页 > 产品设计 > UI/UE

UITableView使用selectRowAtIndexPath不能默认选中cell的问题

2015-09-30 16:43 1111 查看
http://stackoverflow.com/questions/23025120/selectrowatindexpath-from-another-uiviewcontroller-not-working

按照常理,以及文档说明,函数

- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;

就是做的这个事,用来人工选中 cell,但是在UITableViewController中确实无效。

但是发现无效。通过上面的帖子,发现是有个UITableViewController 的属性

clearsSelectionOnViewWillAppear
需要设置为NO,这个默认为 YES。导致每次willAppear的时候,会去清理select的数据。

解释如下:

When the table view is about to appear the first time it’s loaded, the table-view controller reloads
the table view’s data. It also clears its selection (with or without animation, depending on the request) every time the table view is displayed. The UITableViewController class implements this in the superclass method viewWillAppear:. You can disable this
behavior by changing the value in the clearsSelectionOnViewWillAppear property.

修改成如下代码就好了:

[cpp] view
plaincopy

- (void)viewDidLoad

{

[super viewDidLoad];

self.clearsSelectionOnViewWillAppear = NO;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: