[英]UITableView scroll to bottom
我有這行代碼:
tableView.contentOffset = CGPointMake(0.0f, 10000000.0f);
內容大小比10000000.0f
小很多,但 UITableView 仍然沒有滾動到底部。 我該怎么做?
滾動到tableViewCell
?
//for instance, you have 15 cells
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:14 inSection:0];
[self.tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
您可以創建UIScrollView
的擴展,並將其用於 UIScrollView、UITableView 或 UICollectionView 的任何實例。
extension UIScrollView {
func scrollToBottom(animated: Bool) {
var y: CGFloat = 0.0
let HEIGHT = self.frame.size.height
if self.contentSize.height > HEIGHT {
y = self.contentSize.height - HEIGHT
}
self.setContentOffset(CGPointMake(0, y), animated: animated)
}
}
每當內容大小大於滾動視圖的高度時,它將相應地滾動到正確的位置。 此方法也適用於 AutoLayout。
UITableView
,即使有 0 個元素的部分(這會在我在互聯網上看到的所有其他解決方案中崩潰)extension UITableView {
func scrollTableViewToBottom(animated: Bool) {
guard let dataSource = dataSource else { return }
var lastSectionWithAtLeasOneElements = (dataSource.numberOfSections?(in: self) ?? 1) - 1
while dataSource.tableView(self, numberOfRowsInSection: lastSectionWithAtLeasOneElements) < 1 {
lastSectionWithAtLeasOneElements -= 1
}
let lastRow = dataSource.tableView(self, numberOfRowsInSection: lastSectionWithAtLeasOneElements) - 1
guard lastSectionWithAtLeasOneElements > -1 && lastRow > -1 else { return }
let bottomIndex = IndexPath(item: lastRow, section: lastSectionWithAtLeasOneElements)
scrollToRow(at: bottomIndex, at: .bottom, animated: animated)
}
}
本主題中 Kevin 的固定版本,防止無限滾動0
項的 tableView:來源: https : //stackoverflow.com/a/59470799/9763761
func scrollToBottom(animated: Bool) {
guard let dataSource = dataSource else { return }
var lastSectionWithAtLeastOneElement = (dataSource.numberOfSections?(in: self) ?? 1) - 1
while dataSource.tableView(self, numberOfRowsInSection: lastSectionWithAtLeastOneElement) < 1 && lastSectionWithAtLeastOneElement > 0 {
lastSectionWithAtLeastOneElement -= 1
}
let lastRow = dataSource.tableView(self, numberOfRowsInSection: lastSectionWithAtLeastOneElement) - 1
guard lastSectionWithAtLeastOneElement > -1 && lastRow > -1 else { return }
let bottomIndex = IndexPath(item: lastRow, section: lastSectionWithAtLeastOneElement)
scrollToRow(at: bottomIndex, at: .bottom, animated: animated)
}
UIScrollView
,但不工作UITableView
有更多的細胞,然后在一個屏幕可以適應,因為蘋果的怪異內部實現的UITableViewController
:extension UIScrollView {
func scrollToBottom(animated: Bool) {
guard contentSize.height > bounds.size.height else { return }
let bottomOffset = CGPoint(x: 0, y: contentSize.height - bounds.size.height + contentInset.bottom)
setContentOffset(bottomOffset, animated: true)
}
}
不幸的是,我還沒有足夠的代表來發表評論,但是 vol 的回答中有 1 個錯誤,當您在具有 0 個項目的 tableview 上調用它時會導致無限的 while 循環,除此之外它工作得很好! 更正以下代碼:
func scrollToBottom(animated: Bool) {
guard let dataSource = dataSource else { return }
var lastSectionWithAtLeastOneElement = (dataSource.numberOfSections?(in: self) ?? 1) - 1
while dataSource.tableView(self, numberOfRowsInSection: lastSectionWithAtLeastOneElement) < 1 && lastSectionWithAtLeastOneElement > 0 {
lastSectionWithAtLeastOneElement -= 1
}
let lastRow = dataSource.tableView(self, numberOfRowsInSection: lastSectionWithAtLeastOneElement) - 1
guard lastSectionWithAtLeastOneElement > -1 && lastRow > -1 else { return }
let bottomIndex = IndexPath(item: lastRow, section: lastSectionWithAtLeastOneElement)
scrollToRow(at: bottomIndex, at: .bottom, animated: animated)
}
試試這個代碼:
self.tableView.reloadData()
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.1, execute: {
let indexPath = IndexPath(row: self.dateSource.count-1, section: 0)
self.tableView.scrollToRow(at: indexPath, at: UITableViewScrollPosition.bottom, animated: true)
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.