ios - 调用-reloadData后如何保持UITableView contentoffset

标签 ios uitableview reloaddata contentoffset

CGPoint offset = [_table contentOffset];
[_table reloadData];
[_table setContentOffset:offset animated:NO];    //unuseful

//    __block UITableView *tableBlock = _table;
//    [self performBlock:^(id sender) {
//        [tableBlock setContentOffset:offset];
//    } afterDelay:2];

我知道不知道在 reloadData 之后调用的任何委托(delegate)方法。 使用 afterDelay:2 是一种 hack,可能太短或太长,那么我该如何实现呢?

最佳答案

我遇到了这个问题,因为我在 cellForRowAtIndexPath 方法中弄乱了单元格大小。我注意到在执行 reloadData 后大小信息已关闭,所以我意识到我需要在设置内容偏移量之前立即强制它进行布局。

CGPoint offset = tableView.contentOffset;
[tableView.messageTable reloadData];
[tableView layoutIfNeeded]; // Force layout so things are updated before resetting the contentOffset.
[tableView setContentOffset:offset];

关于ios - 调用-reloadData后如何保持UITableView contentoffset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8640409/

相关文章:

ios - 正在使用performSelector :afterDelay: the most efficient way to queue or order methods?

ios - 具有多个数据数组的 UITableView

ios - 将 UITableViewDataSource 与具有 subview 的自定义单元格一起使用

objective-c - 如何使用 ASIHTTPRequest 在自定义 UITableViewCell 中显示下载进度?

ios - tableview reloadData 在展开可选值时意外崩溃发现 nil

iphone - 如何在实现 UITableViewDataSource 协议(protocol)的类中重新加载数据

ios - UICollectionViewCell 未正确设置属性

android - 处理 PDF 以减小文件大小/和/或复杂性

ios - 在 UICollectionView 中创建慢速滚动到 indexPath

ios - KIF 或 Frank 用于自动化 iOS 设备测试