【问题标题】:Why does indexPathForItemAtPoint always return nil为什么 indexPathForItemAtPoint 总是返回 nil
【发布时间】:2013-03-14 06:44:57
【问题描述】:

我有一个UICollectionView 包含在一个使用水平UICollectionViewFlowLayoutUIViewController 中。我还使用了在我的UIStoryboard 中创建的原型单元。我想创建一个自定义分页,所以我使用- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset

问题是每次我试图获取与 targetContentOffset 对应的indexPath (滚动视图中滚动停止的点)时,即使该点显然在集合视图 contentSize。

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

我做错了什么?

【问题讨论】:

  • 你为什么使用 ivar?先检查_collectionView本身是否为nil。
  • 我尝试使用 scrollView 变量本身,但结果是一样的。当我调用该方法时,我很肯定两者都不为零。

标签: iphone ios uicollectionview flowlayout


【解决方案1】:

collectionView 还不知道 targetContentOffset 的 indexPath。

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

试试这样的:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 2017-03-28
    • 2015-03-05
    • 2014-08-25
    • 2016-05-17
    • 1970-01-01
    相关资源
    最近更新 更多