简体   繁体   English

如何将 UITableView 滚动到 tableFooterView

[英]How to scroll a UITableView to the tableFooterView

I have a UITableView whose contents are dynamically changing, like a FIFO stack.我有一个 UITableView,它的内容是动态变化的,就像一个 FIFO 堆栈。 Cells are added to the bottom and removed from the top.单元格添加到底部并从顶部移除。

This works beautifully, and I can scroll to the indexPath so that the newest message always scrolls down to the bottom (Like a chat application).这很好用,我可以滚动到 indexPath,这样最新的消息总是向下滚动到底部(就像聊天应用程序一样)。

Now.. I want to add a footer to that table section.现在.. 我想在那个表格部分添加一个页脚。 Instead of using而不是使用

SrollToRowAtIndexPath SrollToRowAtIndexPath

I would like to be able to scroll to the tableFooterView.我希望能够滚动到 tableFooterView。

Any ideas how I can do that would be appreciated.任何我如何做到这一点的想法将不胜感激。

I am using this to scroll to the footer view of a tableView:我正在使用它滚动到 tableView 的页脚视图:

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

The best way to scroll a UITableView to the bottom of it's footerView is to simply set the content offset.UITableView滚动到其 footerView 底部的最佳方法是简单地设置内容偏移量。 You can calculate the bottom using the contentSize and the current bounds您可以使用contentSize和当前bounds计算底部

Here is the way I do it.这是我做的方式。

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

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

Thanks to iphone_developer here is what I did:感谢 iphone_developer 这是我所做的:

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

Then while I'm adding rows, I'm calling this and my tableView's footer view keeps being visible然后,当我添加行时,我调用它并且我的 tableView 的页脚视图一直可见

Swift Version:斯威夫特版本:

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

So many poor answers.这么多糟糕的答案。 :-) :-)

The BEST way:最好的办法:

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

Maybe something like:也许是这样的:

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

Since UITableView is a subclass of UIScrollView, you can scroll to wherever you like using the UIScrollView method由于 UITableView 是 UIScrollView 的子类,您可以使用 UIScrollView 方法滚动到您喜欢的任何位置

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

Just set the rect so that when it's visible the footer is visible, and you'll have your solution (you can use the footer's rect or something else, just so long as you get the right behavior all the time).只需设置 rect 以便在它可见时页脚可见,您就会得到您的解决方案(您可以使用页脚的 rect 或其他东西,只要您始终获得正确的行为)。

My late answer is for developer, who need to show footer when the keyboard is shown.我迟到的答案是给开发人员的,他们需要在显示键盘时显示页脚。 The right solution is to consider contentInset property (which can be changed after keyboard is shown), so it's super easy:正确的解决方案是考虑 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];
  }
}

I must notice, that in my case tableView was added to my own ViewController and one of cells have UITextField which become first responder.我必须注意,在我的例子中,tableView 被添加到我自己的 ViewController 中,其中一个单元格具有 UITextField,它成为第一响应者。 To move show footer when keyboard is shown you need to register keyboard did shown notification and (on iOS7) perform this method in the end of current run loop, coz in this case iOS7 automatically perform scrollToRowAtIndexPath after our method and footer will not be shown.要在显示键盘时移动显示页脚,您需要注册 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];
}

The easiest way to do this is to use UITableViewScrollPositionTop on the LAST ROW in the LAST SECTION.最简单的方法是在 LAST SECTION 的 LAST ROW 上使用 UITableViewScrollPositionTop。 This works very well for me...这对我来说非常有效......

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

Make sure your Table Footer View is well spaced out at the bottom and it should sit nicely animated inside the view...确保您的 Table Footer View 在底部间隔良好,并且它应该在视图内很好地动画......

Hope this helps...希望这可以帮助...

This good work for me!这对我来说很好!

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

This works for me in Swift 4这在 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)
}

Great idea, was looking for this myself:) Here's sample code, which would do the trick:好主意,我自己也在找这个:)这是示例代码,可以解决问题:

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

You MUST have at least one cell in your tableView footer, the problem is that it's going to visible.您的 tableView 页脚中必须至少有一个单元格,问题是它将可见。 Didn't have time to test, but I'd guess you could make it really small?没有时间测试,但我猜你能把它做得很小吗?

Additionally you must implement correct stuff inside numberOfSectionsInTableView (at least one for table and one for footer), numberOfRowsInSection (at least one for footer, your last section), viewForHeaderInSection (nil except for your cell), heightForHeaderInSection (maybe if you set this as zero), cellForRowAtIndexPath (add special case for your cell in footer)...此外,您必须在numberOfSectionsInTableView (至少一个用于表格,一个用于页脚)、 numberOfRowsInSection (至少一个用于页脚,您的最后一节)、 viewForHeaderInSection (除了您的单元格外为零)、 heightForHeaderInSection (也许如果您将其设置为零), cellForRowAtIndexPath (在页脚中为您的单元格添加特殊情况)...

That should be enough.这应该足够了。

This work to me:这对我有用:

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

I'm using this:我正在使用这个:

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

} }

I haven't tried, but what happens when you scroll to the last row+1?我没试过,但是当你滚动到最后一行+1 时会发生什么?

Another idea would be to always add a dummy entry at the end and make it have a different look so it's seen as a footer.另一个想法是始终在末尾添加一个虚拟条目并使其具有不同的外观,以便将其视为页脚。 Then you can always scroll to that.然后你可以随时滚动到那个。

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

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