首页 文章

在tableView中重新加载后维护滚动位置

提问于
浏览
3

我有一个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 . 他可以在 UITableViewScrollPositionTopUITableViewScrollPositionBottom 完成滚动,甚至可以介于两者之间 .

  • 编辑 -

通过偏移计算也是有问题的,因为偏移是视图开始和顶部表滚动位置之间的差异 . 虽然我对这个值不感兴趣:/ .

1 回答

  • 0

    因为我的结构非常复杂,我完成了这样做(例如,可扩展部分等) . 请记住,我正在保存 _lastSelectedItem ,这是我在datasource的对象,因为indexPath将在刷新后更改 .

    - (void)refresh {
        [self saveLastScrollPosition];
        [self reloadData]; // inside we reload data + reload tableView
        [self scrollToLastScrollPosition];
    }
    
    - (NSInteger)heightToMinYOfCellAtIndexPath:(NSIndexPath *)indexPath {
        NSInteger sections = indexPath.section;
        NSInteger totalRows = 0;
        for (NSInteger section = 0; section < indexPath.section; section++) {
            totalRows += [self.tableView numberOfRowsInSection:section];
        }
        totalRows += indexPath.row;
    
        return ((sections + 1) * kHeaderHeight + totalRows * kRowHeight);
    }
    
    - (void)saveLastScrollPosition {
        if (_lastSelectedItem) { // make sure we have that item selected
            NSIndexPath *indexPath = [self itemForItem:_lastSelectedItem];
            NSInteger aboveItemHeight = [self heightToMinYOfCellAtIndexPath:indexPath];
            CGFloat contentOffsetY = self.tableView.contentOffset.y;
            _previousOffset = aboveItemHeight - contentOffsetY;
        }
    }
    
    - (void)scrollToLastScrollPostion {
        if (_lastSelectedItem) { // make sure we have that item selected
            NSIndexPath *indexPath = [self itemForItem:_lastSelectedItem];
            if (self.tableView.numberOfSections > indexPath.section && [self.tableView numberOfRowsInSection:indexPath.section] > indexPath.row) { // make sure the indexPath still exist after reload
                NSInteger aboveItemHeight = [self heightToMinYOfCellAtIndexPath:indexPath]; // height of items above selected index
                CGPoint offset = CGPointMake(0.f, aboveItemHeight - _previousOffset);
                // in case index should be higher: eg earlier item was at index (4,8) now is at (0,0)
                if (offset.y < 0) {
                    offset.y = 0;
                }
                [self.tableView setContentOffset:offset animated:NO]; // just jump, user shouldn't see this
                _previousOffset = 0; // forget the calculated values
                _lastSelectedItem = nil;
            }
        }
    }
    

相关问题