3
我有一个tableView。当用户点击其中一个记录时,我检查它的属性,并基于此我筛选结果并显示相似的结果 - 所以我需要刷新tableView。重新加载tableView后维护滚动位置查看
问题是,用户可以向上/向下滚动tableView。我需要滚动tableView,以便与刷新之前的单元完全相同UITableViewScrollPosition
。
很显然,我保留最后轻按的项目
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
_lastSelectedItem = [self itemForIndexPath:indexPath];
}
然后重装的tableView后:
NSIndexPath *indexPath = [self indexPathForItem:_lastSelectedItem];
if (_tableView.numberOfSections > indexPath.section && [_tableView numberOfRowsInSection:indexPath.section] > indexPath.row) {
[_tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
_lastSelectedItem = nil;
}
这将是一件好事,但......用户无法在UITableViewScrollPositionMiddle
结束。他本来可以在UITableViewScrollPositionTop
或UITableViewScrollPositionBottom
甚至之间的某个地方完成滚动。
- 编辑 -
通过偏移量计算也是有问题的,因为偏移的观点开始和顶级表滚动位置的区别。虽然我不关心这个值:/。
它可以通过一个'reloadDataAndScrollToLastPosition'公共方法放入UITableView类别中。 – ingaham 2015-01-07 16:54:34
如果假设uitableview单元格高度动态,并且标题也是动态的,我们将使用多个自定义单元格,那么我们可以做些什么来解释它............. – 2015-10-25 13:30:40
@JatiendarKumar解释取决于你是怎么做到的执行它。你可以看一下http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout,在页面中间你可以看到这样的方法'heightForBasicCellAtIndexPath:'并且把它当作你的例。 – Vive 2015-10-26 10:00:51