【问题标题】:Force UITableView to scroll to top?强制 UITableView 滚动到顶部?
【发布时间】:2012-06-29 21:22:18
【问题描述】:

如果 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。它会弹到顶部然后弹回来

【问题讨论】:

    标签: iphone objective-c ios uitableview


    【解决方案1】:

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

    [self.tableView setContentOffset:CGPointZero animated:YES];
    

    【讨论】:

      【解决方案2】:

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

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

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

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

      【讨论】:

      • 我认为问题在于,如果您将一个单元格滚动到顶部,否则如果它位于底部则可见(如果单元格太少),那么它只会反弹回来。
      • 我上面提到的方法将解决保持编辑单元格可见的问题。但是,例如,如果它是最后一个单元格,则当它被关闭时,您将无法阻止该单元格跟随键盘向后退。除了添加虚拟或空单元格之外,您无能为力。
      • 在列表顶部滚动时,如果您还想显示标题,请使用 UITableViewScrollPositionMiddle
      【解决方案3】:

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

      编辑:我确实找到了一些看起来像你正在寻找的东西: Code here.

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

      【讨论】:

      • 我是否只是将源代码 tableview.h 和 .m 文件放入我现有的项目中?我没有使用 UITableViewController,只有 UITableView
      • 我没有使用 .xib 或情节提要...所有内容都使用自给自足的子视图布置在一个视图控制器中(以支持向左/向右滑动动画)
      • 只需获取 TPKeyboardAvoidingTableView.m/.h 文件并将您的 UITableView 实例设置为该实例的实例。您不需要有 xib 或 UITableViewController。或者,您可以从中提取您需要的代码并将其放入您的视图控制器中,而不是对其进行子类化。如果由于某种原因这不是一个合适的解决方案,我会尽力为您提供进一步的帮助。
      • 我不确定我是否正确实现了它,但只是将它放入并更改我的 tableview 不会做任何事情。我所有的 tableview 功能都可以工作,但不会滑动。我忘了说只有在 tableview 处于编辑模式时才会进行编辑
      • 我得到了这个工作,只是它给我带来了另一个问题......我的 tableView 单元格展开,但没有正确折叠。如果我尝试折叠单元格,它实际上会崩溃
      【解决方案4】:

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

      [mainTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
      

      【讨论】:

        【解决方案5】:

        你试过吗? :

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

        样本是:

        [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:0 animated:YES];
        

        【讨论】:

          【解决方案6】:

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

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

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

          【讨论】:

            【解决方案7】:

            滚动到您的tableView 顶部会很有帮助

            tableView.setContentOffset(CGPoint(x: 0, y: -tableView.contentInset.top), animated: true).
            

            【讨论】:

              【解决方案8】:

              对您的问题的更具体的回答:

              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)
                      }
                  }
              }
              

              【讨论】:

                猜你喜欢
                • 2010-10-18
                • 2018-11-02
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2021-06-09
                • 2015-10-04
                • 2016-09-04
                相关资源
                最近更新 更多