ios UITableView 检测最后一个单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3241388/
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
UITableView Detecting Last Cell
提问by Ward
How can I detect when a UITableView
has been scrolled to the bottom so that the last cell is visible?
如何检测 a 何时UITableView
滚动到底部以便最后一个单元格可见?
回答by Michael Kessler
Inside tableView:cellForRowAtIndexPath:
or tableView:willDisplayCell:forRowAtIndexPath:
like this:
里面tableView:cellForRowAtIndexPath:
或者tableView:willDisplayCell:forRowAtIndexPath:
像这样:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
NSInteger sectionsAmount = [tableView numberOfSections];
NSInteger rowsAmount = [tableView numberOfRowsInSection:[indexPath section]];
if ([indexPath section] == sectionsAmount - 1 && [indexPath row] == rowsAmount - 1) {
// This is the last cell in the table
}
...
}
回答by Art Gillespie
Implement the tableView:willDisplayCell:forRowAtIndexPath:
method in your UITableViewDelegate and check to see if it's the last row.
tableView:willDisplayCell:forRowAtIndexPath:
在 UITableViewDelegate 中实现该方法并检查它是否是最后一行。
回答by samwize
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger lastSectionIndex = [tableView numberOfSections] - 1;
NSInteger lastRowIndex = [tableView numberOfRowsInSection:lastSectionIndex] - 1;
if ((indexPath.section == lastSectionIndex) && (indexPath.row == lastRowIndex)) {
// This is the last cell
}
}
回答by Bart?omiej Semańczyk
Create elegant extension for UITableView
:
创建优雅的扩展UITableView
:
extension UITableView {
func isLast(for indexPath: IndexPath) -> Bool {
let indexOfLastSection = numberOfSections > 0 ? numberOfSections - 1 : 0
let indexOfLastRowInLastSection = numberOfRows(inSection: indexOfLastSection) - 1
return indexPath.section == indexOfLastSection && indexPath.row == indexOfLastRowInLastSection
}
}
Example of usage:
用法示例:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView.isLast(for: indexPath) {
//do anything then
}
return cell
}
回答by Shakeel Ahmed
For Xcode 9 and Swift 4/5
对于 Xcode 9 和 Swift 4/5
//Show Last Cell (for Table View)
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
{
if indexPath.row == (yourdataArray.count - 1)
{
print("came to last row")
}
}
//Show last cell (for Collection View)
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath)
{
if indexPath.row == (yourdataArray.count - 1)
{
// Last cell is visible
}
}
回答by shafi
For Xcode 8 and swift 3
对于 Xcode 8 和 swift 3
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let lastSectionIndex = tableView.numberOfSections - 1
let lastRowIndex = tableView.numberOfRows(inSection: lastSectionIndex) - 1
if indexPath.section == lastSectionIndex && indexPath.row == lastRowIndex {
print("this is the last cell")
}
}
回答by J. Doe
I had some problems with the willDisplayCell method. This works for me:
我在使用 willDisplayCell 方法时遇到了一些问题。这对我有用:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let y = scrollView.contentOffset.y/(scrollView.contentSize.height - scrollView.frame.size.height)
let relativeHeight = 1 - (table.rowHeight / (scrollView.contentSize.height - scrollView.frame.size.height))
if y >= relativeHeight{
print("last cell is visible")
}
}
Just pass this in the delegate methods, and change 'table' to your tableView.
只需在委托方法中传递它,然后将“table”更改为您的 tableView。