2013-03-24 382 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];