我有一个包含导航栏、表格 View 和工具栏的 View Controller 。我将 UITableViewDelegate 包含在 View Controller 中,并通过 Storyboard 将表的数据源和委托(delegate)正确分配给 View Controller 。表格 View 从远程数据库加载数据,一旦表格滚动到最后一个单元格,更多数据就会加载到表格中。我通过使用以下帖子中概述的 scrollViewDidScroll 和 indexPathForRowAtPoint 方法实现了这一点:How to know when UITableView did scroll to bottom in iPhone .但是,当我运行应用程序并滚动浏览表时,indexPathForRowAtPoint 返回的唯一索引路径是表加载时位于指定点的路径。这是我滚动时得到的代码和输出:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGPoint bottomPoint = CGPointMake(160, 430);
CGPoint topPoint = CGPointMake(160, 10);
NSLog(@"%d", [[_tableView indexPathForRowAtPoint:bottomPoint] row]);
}
每次滚动都会输出以下内容:
2013-06-08 00:56:45.006 Coffee[24493:907] 3
2013-06-08 00:56:45.012 Coffee[24493:907] 3
2013-06-08 00:56:45.040 Coffee[24493:907] 3
2013-06-08 00:56:45.069 Coffee[24493:907] 3
2013-06-08 00:56:45.088 Coffee[24493:907] 3
2013-06-08 00:56:45.105 Coffee[24493:907] 3
2013-06-08 00:56:45.135 Coffee[24493:907] 3
2013-06-08 00:56:45.144 Coffee[24493:907] 3
2013-06-08 00:56:45.173 Coffee[24493:907] 3
2013-06-08 00:56:45.180 Coffee[24493:907] 3
其中 3 是 Controller 加载时底部点所在单元格的 indexPath.row。我做错了什么,为什么会这样?这与 UITableView 位于父 View Controller 内有什么关系吗?
最佳答案
bottomPoint
正在寻找 scrollView
中的位置。您的 scrollView
包含一个表格,并且所有单元格始终位于与您的 scrollView
相关的同一位置。这就是为什么您总是在那个时候得到相同的单元格。
当您滚动时,单元格不会在 scrollView
中移动,scrollView
会相对于其父级“移动”。这是通过更改其 contentOffset 来完成的。
如果您将 scrollView
的 y 内容偏移量添加到您的 bottomPoint
,您将得到您可能真正在 scrollView< 中寻找的点
.
像这样:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGPoint bottomPoint = CGPointMake(160, 430) - scrollView.contentOffset.y;
CGPoint topPoint = CGPointMake(160, 10);
NSLog(@"%d", [[_tableView indexPathForRowAtPoint:bottomPoint] row]);
}
关于iphone - IOS 问题在特定点获取索引路径行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16996250/