如何在 TableViewController 中使用 indexPathForItemAtPoint?
How to use indexPathForItemAtPoint in TableViewController?
我正在使用 PFQueryTableViewController
,在我的 PFQTVC.m 中,我已经实现了 scrollViewDidScroll
方法,因此我可以在滚动时获得可见单元格的 indexPath
。
但是,我得到
NO VISIBLE INTERFACE FOR 'PFQTVC' DECLARES THE SELECTOR 'INDEXPATHFORITEMATPOINT'.
添加了 UITableViewDataSource
和 UITableViewDelegate
。
怎么办?
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog(@"Did Scroll");
CGRect visibleRect = (CGRect){.origin = self.tableView.contentOffset, .size = self.view.bounds.size};
CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
NSIndexPath *visibleIndexPath = [self indexPathForItemAtPoint:visiblePoint];
NSLog(@"visible Index Path : %@", visibleIndexPath);
}
indexPathForItemAtPoint:
方法来自 UICollectionView
,而非 UITableView
。你想使用 indexPathForRowAtPoint:
。它需要在 table 视图上调用,而不是在控制器上调用。
你想要:
NSIndexPath *visibleIndexPath = [self.tableView indexPathForRowAtPoint:visiblePoint];
我正在使用 PFQueryTableViewController
,在我的 PFQTVC.m 中,我已经实现了 scrollViewDidScroll
方法,因此我可以在滚动时获得可见单元格的 indexPath
。
但是,我得到
添加了NO VISIBLE INTERFACE FOR 'PFQTVC' DECLARES THE SELECTOR 'INDEXPATHFORITEMATPOINT'.
UITableViewDataSource
和 UITableViewDelegate
。
怎么办?
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog(@"Did Scroll");
CGRect visibleRect = (CGRect){.origin = self.tableView.contentOffset, .size = self.view.bounds.size};
CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
NSIndexPath *visibleIndexPath = [self indexPathForItemAtPoint:visiblePoint];
NSLog(@"visible Index Path : %@", visibleIndexPath);
}
indexPathForItemAtPoint:
方法来自 UICollectionView
,而非 UITableView
。你想使用 indexPathForRowAtPoint:
。它需要在 table 视图上调用,而不是在控制器上调用。
你想要:
NSIndexPath *visibleIndexPath = [self.tableView indexPathForRowAtPoint:visiblePoint];