Iphone 为什么indexPathForItemAtPoint总是返回零
我有一个Iphone 为什么indexPathForItemAtPoint总是返回零,iphone,ios,uicollectionview,flowlayout,Iphone,Ios,Uicollectionview,Flowlayout,我有一个UICollectionView包含在使用水平UICollectionViewFlowLayout的UIViewController中。我还使用了在我的UIStoryboard中创建的原型单元。我想创建一个自定义分页,所以我使用了-(void)ScrollViewWillendDraging:(UIScrollView*)scrollView with velocity:(CGPoint)velocity targetContentOffset:(inout CGPoint*)targe
UICollectionView
包含在使用水平UICollectionViewFlowLayout
的UIViewController
中。我还使用了在我的UIStoryboard
中创建的原型单元。我想创建一个自定义分页,所以我使用了-(void)ScrollViewWillendDraging:(UIScrollView*)scrollView with velocity:(CGPoint)velocity targetContentOffset:(inout CGPoint*)targetContentOffset
问题是,每次我试图获取与targetContentOffset(滚动视图中滚动将停止的点)对应的indexPath
,集合视图都会返回nil,即使该点明显位于集合视图contentSize内
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
NSIndexPath *indexPath = [_collectionView indexPathForItemAtPoint:(*targetContentOffset)];
// Do something with indexPath
}
我做错了什么?collectionView还不知道targetContentOffset的indexPath 您必须要求collectionViewLayout为您提供将在此位置进行布局的项目的属性 试着这样做:
UICollectionViewLayoutAttributes *attribute = [_collectionView.collectionViewLayout layoutAttributesForElementsInRect:(CGRect){(*targetContentOffset).x, 0, 10, self.collectionView.bounds.size.height}][0];
你为什么要用ivar?首先检查
\u collectionView
本身是否为nil。我尝试使用scrollView变量本身,但结果相同。我肯定当我调用这个方法时,两者都不是零。