[英]iOS UITableView Scroll to bottom of section
我將創建一個包含2個部分的表格視圖。 我可以以編程方式將單元格添加到每個部分,當我添加時,我使用滾動到tableview的末尾
[_tableView setContentOffset:CGPointMake(0, CGFLOAT_MAX)];
我的問題是如何滾動到第0節的末尾,以便當用戶將單元格添加到第0節時,tableview動態滾動到第0節的最后一個單元格。
謝謝。
您可以嘗試使用以下代碼:
int yourSection = 2;
int lastRow = [tableView numberOfRowsInSection:yourSection] - 1;
[tableView scrollToRowAtIndexPath:[NSIndexPath lastRow inSection:yourSection] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
您將獲得部分中的行數,然后滾動到該indexPath。
上面的所有建議都是正確的,至少基於文檔。 但這在我的情況下不起作用-我無法使滾動顯示表格視圖的最后一行。 我必須在滾動中添加延遲才能使其正常工作。
- (void)scrollToTheBottom:(BOOL)animated
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowCount-1 inSection:0];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:animated];
}
我稱以上為:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self scrollToTheBottom:YES];
});
當在末尾插入行時,具有其索引路徑,可以使用tableview的scrollToIndexPath方法滾動
[self.liveChannelsTable scrollToRowAtIndexPath:IndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
以防萬一,這是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)
}
}
在Swift 3中,它已更新為:
let indexPath = IndexPath(row: self.numberOfRowsInSection(0) - 1), section: 0)
self.commentsTableView.scrollToRow(at: indexPath, at: .bottom, animated: false)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.