【问题标题】:How to scroll a UITableView to the tableFooterView如何将 UITableView 滚动到 tableFooterView
【发布时间】:2010-11-24 05:59:05
【问题描述】:

我有一个 UITableView,它的内容是动态变化的,就像一个 FIFO 堆栈。单元格被添加到底部并从顶部移除。

这很好用,我可以滚动到 indexPath 以便最新消息总是向下滚动到底部(就像聊天应用程序一样)。

现在.. 我想在该表格部分添加页脚。而不是使用

SrollToRowAtIndexPath

我希望能够滚动到 tableFooterView。

任何想法我可以如何做到这一点将不胜感激。

【问题讨论】:

  • 您使用的最佳解决方案是什么?>

标签: iphone uitableview


【解决方案1】:

我正在使用它来滚动到 tableView 的页脚视图:

[self.tableView scrollRectToVisible:[self.tableView convertRect:self.tableView.tableFooterView.bounds fromView:self.tableView.tableFooterView] animated:YES];

【讨论】:

  • 简单易用的任何地方!
  • +1!这是正确答案,我测试的所有其他答案在某些情况下都无法正常工作。
  • 理论上这很好,但实际上它不能与表视图更新链接在一起——比如在表视图动画时插入和删除行。 scrollToIndexPath 适用于此
【解决方案2】:

UITableView 滚动到其footerView 底部的最佳方法是简单地设置内容偏移量。您可以使用contentSize 和当前的bounds 计算底部

这是我的做法。

CGPoint newContentOffset = CGPointMake(0, [self.instanceOfATableView contentSize].height -  self.instanceOfATableView.bounds.size.height);

[self.instanceOfATableView setContentOffset:newContentOffset animated:YES]; 

【讨论】:

  • 我只有在调用 reloadTable: [self performSelector:@selector(scrollToBottom) withObject:nil afterDelay:0.3] 后添加延迟时才设法使其工作
【解决方案3】:

感谢 iphone_developer,这就是我所做的:

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[tableView numberOfRowsInSection:0]-1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];

然后当我添加行时,我调用它并且我的 tableView 的页脚视图保持可见

Swift 版本:

tableView.scrollToRow(at: IndexPath(row: self.tblview.numberOfRows(inSection: 0) - 1, section: 0), at: .top, animated: true)

【讨论】:

  • 这将滚动到顶部而不是底部
  • 不,[tableView numberOfRowsInSection:0]-1 是最后一行,所以滚动到这一行的 positionTop 实际上会将 tableView 滚动到底部
  • @Beji 这假设你只有一个部分
  • 这很有趣,因为我正在这样做,但滚动到底部,隐藏了页脚。使用滚动位置顶部调用它。谢谢
【解决方案4】:

这么多糟糕的答案。 :-)

最好的方法:

[self.tableView scrollRectToVisible:
     self.tableView.tableFooterView.frame animated:YES
];

【讨论】:

  • 这假定tableFooterView 将始终是tableView 的直接子视图,或者它位于任何中间视图的原点。虽然现在这可能有效,但不能保证 tableView 子视图层次结构在未来的 iOS 版本中会保持不变。
  • @Kumar KL ,这不适用于自定义页脚和多个页脚。
【解决方案5】:

可能是这样的:

[tableView setContentOffset:CGPointMake(0, tableView.contentSize.height) animated:YES];

【讨论】:

    【解决方案6】:

    由于 UITableView 是 UIScrollView 的子类,你可以使用 UIScrollView 方法滚动到任何你喜欢的地方

    - (void)scrollRectToVisible:(CGRect)rect 动画:(BOOL)animated

    只需设置矩形,这样当它可见时,页脚也可见,你就会有你的解决方案(你可以使用页脚的矩形或其他东西,只要你一直得到正确的行为)。

    【讨论】:

      【解决方案7】:

      我较晚的答案是针对开发人员,他们需要在显示键盘时显示页脚。 正确的解决方案是考虑 contentInset 属性(可以在显示键盘后更改),因此非常简单:

      - (void)scrollToFooter {
        UIEdgeInsets tableInsets = self.tableView.contentInset;
        CGFloat tableHeight = self.tableView.frame.size.height - tableInsets.bottom - tableInsets.top;
        CGFloat bottom = CGRectGetMaxY(self.tableView.tableFooterView.frame);
        CGFloat offset = bottom - tableHeight;
        if(offset > 0.f) {
          [self.tableView setContentOffset:CGPointMake(0,  offset) animated:YES];
        }
      }
      

      我必须注意到,在我的情况下,tableView 已添加到我自己的 ViewController 中,其中一个单元格具有 UITextField,它成为第一响应者。要在显示键盘时移动显示页脚,您需要注册键盘确实显示通知并(在 iOS7 上)在当前运行循环结束时执行此方法,因为在这种情况下,iOS7 会在我们的方法和页脚不会显示后自动执行 scrollToRowAtIndexPath。

      -(void)registerKeyboardNotification {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardDidShown:)
                                                     name:UIKeyboardDidShowNotification object:nil];
      }
      
      - (void)keyboardDidShown:(id)notification {
        //move to the end of run loop
        [self performSelector:@selector(scrollToFooter) withObject:nil afterDelay:.0];
      }
      

      【讨论】:

        【解决方案8】:

        最简单的方法是在 LAST SECTION 的 LAST ROW 上使用 UITableViewScrollPositionTop。这对我来说效果很好......

        [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:LAST_ROW inSection:LAST_SECTION] atScrollPosition:UITableViewScrollPositionTop animated:YES];
        

        确保您的表格页脚视图在底部有很好的间隔,并且它应该在视图内很好地动画...

        希望这会有所帮助...

        【讨论】:

          【解决方案9】:

          这对我来说很好!

          CGRect footerBounds = [addCommentContainer bounds];
          CGRect footerRectInTable = [tableview convertRect:footerBounds fromView:addCommentContainer];
          [tableview scrollRectToVisible:footerRectInTable animated:YES];
          

          【讨论】:

            【解决方案10】:

            这在 Swift 4 中适用于我

            func addRow() {
                tableView.beginUpdates()
                tableView.insertRows(at: [IndexPath(row: array.count - 1, section: 0)], with: .automatic)
                tableView.endUpdates()
            
                DispatchQueue.main.async {
                    self.scrollToBottom()
                }
            }
            
            func scrollToBottom() {
                let footerBounds = tableView.tableFooterView?.bounds
                let footerRectInTable = tableView.convert(footerBounds!, from: tableView.tableFooterView!)
                tableView.scrollRectToVisible(footerRectInTable, animated: true)
            }
            

            【讨论】:

            • 我在检查这段代码,你为什么需要使用 DispatchQueue.main.async {}?
            【解决方案11】:

            好主意,我自己也在找这个:) 这是示例代码,可以解决问题:

            [tableView reloadData];
            NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:1];
            [tableView scrollToRowAtIndexPath:index
                atScrollPosition:UITableViewScrollPositionBottom animated:YES];
            

            您的 tableView 页脚中必须至少有一个单元格,问题是它会可见。没时间测试,但我猜你可以把它做得很小?

            此外,您必须在 numberOfSectionsInTableView(至少一个用于表格,一个用于页脚)、numberOfRowsInSection(至少一个用于页脚,您的最后一部分)中实现正确的东西, viewForHeaderInSection(除您的单元格外为零),heightForHeaderInSection(可能如果您将其设置为零),cellForRowAtIndexPath(为您的单元格添加特殊情况页脚)...

            应该够了。

            【讨论】:

              【解决方案12】:

              我正在使用这个:

              - (void)scrollToBottom{
                 [self.myTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[self.fetchedResultsController.fetchedObjects count] -1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
              

              }

              【讨论】:

                【解决方案13】:

                这项工作对我来说:

                - (void)tableViewScrollToBottomAnimated:(BOOL)animated {
                    NSInteger numberOfRows = [self.tableView numberOfRowsInSection:0];
                    if (numberOfRows) {
                        if (self.tableView.tableFooterView) {
                            [self.tableView scrollRectToVisible:
                             self.tableView.tableFooterView.frame animated:YES
                             ];
                        } else {
                            [self.tableView scrollToRowAtIndexPath:
                             [NSIndexPath indexPathForRow:numberOfRows-1 inSection:0]
                                                  atScrollPosition:UITableViewScrollPositionBottom
                                                          animated:animated
                             ];
                        }
                    }
                }
                

                【讨论】:

                  【解决方案14】:

                  我没试过,但是当你滚动到最后一行+1时会发生什么?

                  另一个想法是始终在末尾添加一个虚拟条目并使其具有不同的外观,以便将其视为页脚。然后你总是可以滚动到那个。

                  【讨论】:

                  • 你得到一个超出范围的错误:(我应该提到我已经在原始帖子中尝试过。
                  猜你喜欢
                  • 2016-06-27
                  • 2012-04-01
                  • 1970-01-01
                  • 2011-08-16
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多