Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 为什么indexPathForItemAtPoint总是返回零_Iphone_Ios_Uicollectionview_Flowlayout - Fatal编程技术网

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变量本身,但结果相同。我肯定当我调用这个方法时,两者都不是零。