2013-03-24 129 views
5

我有一个UICollectionView包含在UIViewController使用水平UICollectionViewFlowLayout。我还使用在UIStoryboard中创建的原型单元格。我想创建一个自定义分页,所以我使用- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset为什么indexPathForItemAtPoint总是返回零

问题是,我每次试图获取对应于targetContentOffset(滚动视图中将停止滚动的点)的indexPath,集合视图将返回nil,即使点明显位于集合内部查看contentSize。

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset 
{ 
    NSIndexPath *indexPath = [_collectionView indexPathForItemAtPoint:(*targetContentOffset)]; 
    // Do something with indexPath  
} 

我在做什么错?

+0

为什么您使用的是伊娃?首先检查'_collectionView'本身是否为零。 – 7usam 2013-03-24 21:07:05

+0

我试过使用scrollView变量本身,但结果是一样的。当我打电话给我的方法时,我都很乐观。 – DrMonkey68 2013-03-24 22:25:46

回答

0

collectionView并不知道targetContentOffset的indexPath。

您必须要求collectionViewLayout为您提供将在此位置布局的项目属性。

尝试这样:

UICollectionViewLayoutAttributes *attribute = [_collectionView.collectionViewLayout layoutAttributesForElementsInRect:(CGRect){(*targetContentOffset).x, 0, 10, self.collectionView.bounds.size.height}][0];