3

如果 tableview 包含少于 10 个左右的单元格,你将如何强制 UITableViewCell 滚动到顶部?当 tableview 处于编辑模式时,我支持在 tableView 单元格中编辑托管对象上下文。不用说,如果一个单元格位于 tableview 的底部,当用户去编辑事件的标题或位置时,它会被键盘阻止。我尝试了一个解决方案,例如:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    if(_selectedIndex == indexPath.row){
        _selectedIndex = -1;
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                         withRowAnimation:UITableViewRowAnimationFade];
        return;
    }

    if(_selectedIndex >= 0){
        NSIndexPath *previous = [NSIndexPath indexPathForRow:_selectedIndex inSection:0];
        _selectedIndex = indexPath.row;
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:previous]
                         withRowAnimation:UITableViewRowAnimationFade];
    }

    _selectedIndex = indexPath.row;
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                     withRowAnimation:UITableViewRowAnimationFade];

    [tableView setContentOffset:CGPointMake(0, [tableView rowHeight]*indexPath.row) animated:YES];
}

但这不会将 tableview 保持在 contentOffset。它会弹到顶部然后弹回

4

8 回答 8

12

我喜欢以下解决方案,因为如果您有标题视图,它还会滚动到标题视图的顶部。

[self.tableView setContentOffset:CGPointZero animated:YES];
于 2013-10-17T07:19:36.867 回答
4

你可能应该在 UITableView 上使用这个方法:

-(void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated

本质上,您所做的是,当 UITableViewDelegate 的 [tableView:didSelectRowAtIndexPath:] 方法被调用时,您将该选择的索引传递给上述方法并设置 scrollposition = UITableViewScrollPositionTop。

这会将选定的单元格滚动到屏幕顶部并避开键盘。

于 2012-06-29T22:04:50.093 回答
2

您是否考虑将整个 tableview 向上滑动?

编辑:我确实找到了一些看起来像你正在寻找的东西: 代码在这里

此代码从底部缩小表格视图的插图,然后允许它显示其中一个较低的单元格而无需向下折回。

于 2012-06-29T21:28:59.310 回答
2

UITableView 是 UIScrollView 的子类,所以你也可以使用:

[mainTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
于 2013-12-24T10:48:20.037 回答
0

你试过这个吗?:

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

样本是:

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:0 animated:YES];
于 2014-04-16T18:34:35.350 回答
0

这在我的情况下有效,它也会滚动到headear的顶部如果你有:

  **myTablView.scrollRectToVisible((myTablView.rectForHeader(inSection: 0), to: myTablView.superview).0, animated: true)**

https://developer.apple.com/documentation/uikit/uitableview/1614872-rectforheader

于 2017-09-07T16:03:27.780 回答
0

更具体的回答您的问题:

extension UITableView {

    func scrollToBottom(){

        DispatchQueue.main.async {
            let indexPath = IndexPath(
                row: self.numberOfRows(inSection:  self.numberOfSections -1) - 1, 
                section: self.numberOfSections - 1)
            self.scrollToRow(at: indexPath, at: .bottom, animated: true)
        }
    }

    func scrollToTop() {

        DispatchQueue.main.async {
            let indexPath = IndexPath(row: 0, section: 0)
            self.scrollToRow(at: indexPath, at: .top, animated: false)
        }
    }
}
于 2019-05-27T10:49:54.007 回答
0

滚动到顶部会很有帮助tableView

tableView.setContentOffset(CGPoint(x: 0, y: -tableView.contentInset.top), animated: true).
于 2019-05-27T10:19:28.273 回答