在UITableView上完成reloaddata之后选择行

时间:2017-06-16 18:16:42

标签: ios objective-c iphone uitableview

我有一个带有自定义UITableViewCell和数据源的UITableView。 我的tableviewdelegate类被注册为对事件进行回调,一旦我收到事件,我就会在tableview上重新加载数据。

在编辑tableviewcell数据之后回调。但是一旦回调来了,当我重新加载数据时,tableview滚动到顶部;而不是留在目前的选择。

所以我缓存了当前选中的单元格并尝试scrollToRowAtIndexPath。

但是我遇到了崩溃。

如何在加载tabledata后滚动到该行?

{

[m_tableView reloadData];

    if(m_currentEditingCell != nullptr)
    {
        [m_tableView scrollToRowAtIndexPath:m_currentEditingCell atScrollPosition:UITableViewScrollPositionNone animated:YES];
    }

}

2 个答案:

答案 0 :(得分:0)

您应提供 NSIndexPath ,而不是将tableviewcell作为选择器 scrollToRowAtIndexPath:atScrollPosition:animated:

的参数提供
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[yourTableView scrollToRowAtIndexPath:indexPath 
                     atScrollPosition:UITableViewScrollPositionTop 
                             animated:YES];

答案 1 :(得分:0)

以下代码可以在重新加载后选择单元格。

{  [m_tableView reloadData]; dispatch_async(dispatch_get_main_queue(),^ {NSIndexPath * indexPath = [NSIndexPath indexPathForRow:([m_tableView numberOfRowsInSection:([m_tableView numberOfSections] -1)] - 1)inSection:([m_tableView numberOfSections] -1)]; if(m_currentCell!= nullptr)[self selectCurrentCell:m_currentCell];

}

}