5

我有一个UICollectionView包含在一个UIViewController使用水平的UICollectionViewFlowLayout. 我还使用在我的UIStoryboard. 我想创建一个自定义分页,所以我使用- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset.

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

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

我究竟做错了什么?

4

1 回答 1

0

collectionView 还不知道 targetContentOffset 的 indexPath。

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

尝试这样的事情:

UICollectionViewLayoutAttributes *attribute = [_collectionView.collectionViewLayout layoutAttributesForElementsInRect:(CGRect){(*targetContentOffset).x, 0, 10, self.collectionView.bounds.size.height}][0];
于 2013-07-28T21:46:50.147 回答