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

来源:互联网 发布:linux开机选择系统 编辑:程序博客网 时间:2024/05/22 02:06

   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.

修改成如下代码就好了:
- (void)viewDidLoad{    [super viewDidLoad];   self.clearsSelectionOnViewWillAppear = NO;}



1 1
原创粉丝点击