快速获取collectionView中心的单元格的indexPath?

eechimosu

我试图通过使用uicollectionview的扩展来获取集合视图中心的单元格索引,但是我总是得到nil而不是索引。我怎样才能解决这个问题?

extension UICollectionView {
    var centerPoint : CGPoint {
        get {
            return CGPoint(x: self.center.x + self.contentOffset.x, y: self.center.y + self.contentOffset.y);
        }
    }

    var centerCellIndexPath: NSIndexPath? {
        if let centerIndexPath: NSIndexPath  = self.indexPathForItemAtPoint(self.centerPoint) {
            return centerIndexPath
        }

        return nil
    }
}

然后:在UIViewController中的随机方法中,我有这个:

if let centerCellIndexPath: NSIndexPath  = collectionTemp!.centerCellIndexPath {
    print(centerCellIndexPath)
} else {
    println("nil")
}

索引路径始终为nil,我不明白为什么会这样,因为单元格按顺序显示,除此以外,一切都很好。

eechimosu

我设法通过使用始终将单元格保持在中心的自定义布局解决了我的问题。使用它,中心的indexPath永远不会为零。

 class CenterFlowLayout: UICollectionViewFlowLayout {

 override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
     if let cv = self.collectionView {
        let cvBounds = cv.bounds
        let halfWidth = cvBounds.size.width * 0.5
        let proposedContentOffsetCenterX = proposedContentOffset.x + halfWidth
     if let attributesForVisibleCells = self.layoutAttributesForElementsInRect(cvBounds) as [UICollectionViewLayoutAttributes]! {
        var candidateAttributes: UICollectionViewLayoutAttributes?
        for attributes in attributesForVisibleCells {
      // == Skip comparison with non-cell items (headers and footers) == //
      if attributes.representedElementCategory != UICollectionElementCategory.Cell {
        continue
      }
      if let candAttrs = candidateAttributes {
        let a = attributes.center.x - proposedContentOffsetCenterX
        let b = candAttrs.center.x - proposedContentOffsetCenterX
            if fabsf(Float(a)) < fabsf(Float(b)) {
               candidateAttributes = attributes
            }
      } else { // == First time in the loop == //
        candidateAttributes = attributes
        continue
      }
    }
    return CGPoint(x : candidateAttributes!.center.x - halfWidth, y : proposedContentOffset.y)
  }
}
// Fallback
    return super.targetContentOffsetForProposedContentOffset(proposedContentOffset)
 }
}

将其添加为UICollectionFlowLayout的子类。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何获取位于UICollectionView中心的单元格的indexPath

来自分类Dev

CollectionView动态单元格高度快速

来自分类Dev

如何快速获得选定单元格的IndexPath

来自分类Dev

向左滑动以选择CollectionView单元格(快速键5)

来自分类Dev

使用自定义单元格获取UITableViewCell indexPath

来自分类Dev

iOS:UICollectionview,如何通过indexPath获取单元格的contentoffset

来自分类Dev

获取嵌入在UITableViewCell中的UITable的单元格的indexPath

来自分类Dev

在单元格中获取 indexPath.row

来自分类Dev

表视图单元格中的UIGestureRecognizer获取indexPath始终返回最后一个单元格的indexPath

来自分类Dev

获取collectionView中的所有单元格

来自分类Dev

获取collectionView中的所有单元格

来自分类Dev

从 UITableviewCell 获取 CollectionView 单元格的屏幕位置

来自分类Dev

从collectionView单元格获取并更新UITableView单元格,单击

来自分类Dev

collectionView单元格重叠

来自分类Dev

快速表格视图单元格不允许我使用 indexPath.row

来自分类Dev

如何快速获取UICollectionView中的单元格atIndex?

来自分类Dev

如何知道单元格SWIFT的indexPath

来自分类Dev

CollectionView会自动快速移至下一个单元格

来自分类Dev

collectionView单元格滚动时图像变化-快速-以编程方式

来自分类Dev

快速过滤tableview数组时,tableview单元格中的collectionview不会刷新

来自分类Dev

快速选择的快速单元格

来自分类Dev

iOS:UICollectionview,如何通过它的indexPath获取单元格的contentoffset

来自分类Dev

如何从委托的收集视图单元格中获取indexPath项?

来自分类Dev

轻按单元格中的UIImageView时如何获取indexpath.row?

来自分类Dev

在单元格创建/出队期间获取 tableViewCell 文件中 tableView 的 indexPath

来自分类Dev

Xib的collectionview单元格未显示

来自分类Dev

CollectionView,更改单元格颜色

来自分类Dev

更新未选择的CollectionView单元格?

来自分类Dev

CollectionView中单元格之间的间距

Related 相关文章

  1. 1

    如何获取位于UICollectionView中心的单元格的indexPath

  2. 2

    CollectionView动态单元格高度快速

  3. 3

    如何快速获得选定单元格的IndexPath

  4. 4

    向左滑动以选择CollectionView单元格(快速键5)

  5. 5

    使用自定义单元格获取UITableViewCell indexPath

  6. 6

    iOS:UICollectionview,如何通过indexPath获取单元格的contentoffset

  7. 7

    获取嵌入在UITableViewCell中的UITable的单元格的indexPath

  8. 8

    在单元格中获取 indexPath.row

  9. 9

    表视图单元格中的UIGestureRecognizer获取indexPath始终返回最后一个单元格的indexPath

  10. 10

    获取collectionView中的所有单元格

  11. 11

    获取collectionView中的所有单元格

  12. 12

    从 UITableviewCell 获取 CollectionView 单元格的屏幕位置

  13. 13

    从collectionView单元格获取并更新UITableView单元格,单击

  14. 14

    collectionView单元格重叠

  15. 15

    快速表格视图单元格不允许我使用 indexPath.row

  16. 16

    如何快速获取UICollectionView中的单元格atIndex?

  17. 17

    如何知道单元格SWIFT的indexPath

  18. 18

    CollectionView会自动快速移至下一个单元格

  19. 19

    collectionView单元格滚动时图像变化-快速-以编程方式

  20. 20

    快速过滤tableview数组时,tableview单元格中的collectionview不会刷新

  21. 21

    快速选择的快速单元格

  22. 22

    iOS:UICollectionview,如何通过它的indexPath获取单元格的contentoffset

  23. 23

    如何从委托的收集视图单元格中获取indexPath项?

  24. 24

    轻按单元格中的UIImageView时如何获取indexpath.row?

  25. 25

    在单元格创建/出队期间获取 tableViewCell 文件中 tableView 的 indexPath

  26. 26

    Xib的collectionview单元格未显示

  27. 27

    CollectionView,更改单元格颜色

  28. 28

    更新未选择的CollectionView单元格?

  29. 29

    CollectionView中单元格之间的间距

热门标签

归档