繁体   English   中英

如何将 UITableView 滚动到 tableFooterView

[英]How to scroll a UITableView to the tableFooterView

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

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

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

SrollToRowAtIndexPath

我希望能够滚动到 tableFooterView。

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

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

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

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

这是我做的方式。

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

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

感谢 iphone_developer 这是我所做的:

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

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

斯威夫特版本:

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

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

最好的办法:

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

也许是这样的:

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

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

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

只需设置 rect 以便在它可见时页脚可见,您就会得到您的解决方案(您可以使用页脚的 rect 或其他东西,只要您始终获得正确的行为)。

我迟到的答案是给开发人员的,他们需要在显示键盘时显示页脚。 正确的解决方案是考虑 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,它成为第一响应者。 要在显示键盘时移动显示页脚,您需要注册 keyboard did shown notification 并且(在 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];
}

最简单的方法是在 LAST SECTION 的 LAST ROW 上使用 UITableViewScrollPositionTop。 这对我来说非常有效......

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

确保您的 Table Footer View 在底部间隔良好,并且它应该在视图内很好地动画......

希望这可以帮助...

这对我来说很好!

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

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

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

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

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

此外,您必须在numberOfSectionsInTableView (至少一个用于表格,一个用于页脚)、 numberOfRowsInSection (至少一个用于页脚,您的最后一节)、 viewForHeaderInSection (除了您的单元格外为零)、 heightForHeaderInSection (也许如果您将其设置为零), cellForRowAtIndexPath (在页脚中为您的单元格添加特殊情况)...

这应该足够了。

这对我有用:

- (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
             ];
        }
    }
}

我正在使用这个:

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

}

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM