iOS UITableView 滚动到部分底部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26160484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:58:13  来源:igfitidea点击:

iOS UITableView Scroll to bottom of section

iosxcodeuitableviewnsindexpath

提问by Mostafa A. Khattab

I am going to make a tableview with 2 sections inside it. I can add cells to every section programmatically and when i add, i scroll to the end of tableview using

我将制作一个包含 2 个部分的 tableview。我可以以编程方式将单元格添加到每个部分,当我添加时,我使用

[_tableView setContentOffset:CGPointMake(0, CGFLOAT_MAX)];

My question is how can i scroll to the end of the section 0, so that when user add a cell to section 0, tableview scroll dynamically to the last cell in section 0.

我的问题是如何滚动到第 0 部分的末尾,以便当用户将单元格添加到第 0 部分时,tableview 会动态滚动到第 0 部分的最后一个单元格。

thanks.

谢谢。

回答by Benetz

You can try with this code:

您可以尝试使用以下代码:

int yourSection = 2;
int lastRow = [tableView numberOfRowsInSection:yourSection] - 1;
[tableView scrollToRowAtIndexPath:[NSIndexPath lastRow inSection:yourSection] atScrollPosition:UITableViewScrollPositionBottom animated:YES];

You get the numbers of rows in your section, then scroll to that indexPath.

您将获得部分中的行数,然后滚动到该 indexPath。

回答by Shirish Kumar

All the suggestions above are correct, at least based on the docs. But it did not work in my case - I could not get the scroll to show the last row in the table view. I had to add a delay in scrolling to make that work.

以上所有建议都是正确的,至少基于文档。但它在我的情况下不起作用 - 我无法滚动以显示表格视图中的最后一行。我不得不在滚动中添加延迟才能使其工作。

- (void)scrollToTheBottom:(BOOL)animated
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowCount-1 inSection:0];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:animated];
}

I called the above as:

我把上面的称为:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self scrollToTheBottom:YES];
});

回答by Adnan Aftab

when you inserting row at end you have its index path, you can use scrollToIndexPath method of tableview to scroll

当你在最后插入行时,你有它的索引路径,你可以使用 tableview 的 scrollToIndexPath 方法滚动

[self.liveChannelsTable scrollToRowAtIndexPath:IndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

回答by Valentin Shergin

Just in case, this is solution in Swift:

以防万一,这是 Swift 中的解决方案:

extension UITableView {
    func scrollToBottom(animated: Bool = true) {
        let sections = self.numberOfSections
        let rows = self.numberOfRowsInSection(sections - 1)
        self.scrollToRowAtIndexPath(NSIndexPath(forRow: rows - 1, inSection: sections - 1), atScrollPosition: .Bottom, animated: true)
    }
}

回答by Daniel Que

In Swift 3 it's been updated to:

在 Swift 3 中,它已更新为:

let indexPath = IndexPath(row: self.numberOfRowsInSection(0) - 1), section: 0)
self.commentsTableView.scrollToRow(at: indexPath, at: .bottom, animated: false)