如何检测UITableView
何时滚动到底部以便最后一个单元格可见?UITableView检测最后一个单元格
回答
在你的UITableViewDelegate中实现tableView:willDisplayCell:forRowAtIndexPath:
方法,并检查它是否是最后一行。
内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
}
...
}
你可能会说我是个假人。感谢帮助有时我觉得这些事情比他们更难。 – Ward 2010-07-13 22:16:01
numberOfSections调用数据源numberOfSectionsInTableView和numberOfRowsInSection调用数据源numberOfRowsInSection。在我的实现中它崩溃了,显示了这个执行的递归日志。 – 2012-12-11 06:28:23
- (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
}
}
对于Xcode的8快捷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")
}
}
对于Xcode中8和Swift3
//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
}
}
创建优雅延伸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
}
}
使用示例:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView.isLast(for: indexPath) {
//do anything then
}
return cell
}
有一些问题与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")
}
}
只要在委托方法中传递此值,并将'table'更改为您的tableView。
- 1. 删除最后一个单元格后没有选择UITableView单元格
- 2. 防止在移除最后一个单元格后滚动uitableview
- 3. 检测UITableView单元格中的Tap&Hold
- 4. UItableview添加新单元格后,前一个单元格消失
- 5. 设置滚动最后UItableview单元格
- 6. 由UITabBarController隐藏的UITableView最后一个单元格
- 7. UITableView自定义accessoryView只出现在最后一个单元格
- 8. 滚动最后一个单元格到UITableView顶部
- 9. 获取UITableview部分中的最后一个单元格
- 10. ios swift uitableview添加最后一个单元格
- 11. 检查UITableView中的多个单元格
- 12. 检测另一个单元格内的单元格选择
- 13. 如何确定一个UITableView的最后一个单元格是屏幕
- 14. UITableView - 当滚动速度过快时,最后一个单元格的内容会替换第一个单元格
- 15. 如何检测UITableView中有多少个单元格可见
- 16. Excel最后一个单元格错误
- 17. 展开/折叠最后一个单元格后奇怪的UITableView行为
- 18. Jquery列单元格的总和在最后一个单元格
- 19. 检测最宽的单元格w/jQuery
- 20. 获得最后一列最后一个单元格的值
- 21. 如何检测何时未选择UITableView表格单元格?
- 22. UITableView - 当上面有一定数量的单元格时,删除最后一个单元格
- 23. 在一个单元格中检测单个或多个单词?
- 24. 添加一个UIPickerView到UITableView单元格
- 25. UITableView隐藏第一个单元格
- 26. UITableView不滚动第一个单元格
- 27. UITableView多个单元格
- 28. UITableView:突出显示最后一个单元格,但其他单元格也突出显示
- 29. UITableview始终为所有单元格加载最后一个对象
- 30. UITableView在每个单元格中显示数组中的最后一项
感谢您的提示。我实际上使用这种方法来触发一个事件,它看起来像是因为它会显示“Will”,它发生在它实际显示单元格之前。显示单元格后是否触发了一种方法? – Ward 2010-07-14 01:41:09
唉,没有。你可以通过使用'performSelector:withObject:afterDelay'稍微延迟一点来调用一个你需要的方法。 – 2010-07-14 02:29:13
@ArtGillespie找到了我正在寻找的东西。干杯! – 2017-05-23 12:51:46