【问题标题】:Get Visible IndexPath UICollectionView in Swift在 Swift 中获取可见的 IndexPath UICollectionView
【发布时间】:2016-03-02 07:09:37
【问题描述】:

如何在collectionView 中获取scrolling 时获得visible IndexPath,我推荐了很多link1,link2 但Swift 不支持indexPathForCell

【问题讨论】:

    标签: ios swift uicollectionview


    【解决方案1】:

    你试过委托功能吗?

    public func indexPathsForVisibleItems() -> [NSIndexPath]
    

    collectionView.indexPathsForVisibleItems()
    

    这些必须给你你想要的。

    【讨论】:

      【解决方案2】:

      Swift,获取可见项目的更安全方式

      if let indexPaths = self.collectionView.indexPathsForVisibleItems {
          //Do something with an indexPaths array.
      }
      

      【讨论】:

        【解决方案3】:

        试试这个

        委托

        func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        for cell in yourCollectionViewname.visibleCells()  as [UICollectionViewCell]    {
           let indexPath = yourCollectionViewname.indexPathForCell(cell as UICollectionViewCell)
        
            NSLog("%@", indexPath)
        }
        }
        

        选择 2

        按钮点击

          var point : CGPoint = sender.convertPoint(CGPointZero, toView:yourCollectionViewname)
        var indexPath =yourCollectionViewname!.indexPathForItemAtPoint(point)
        

        显示所有项目

        您可以使用 indexPathsForVisibleRows

        返回一个索引路径数组,每个索引路径标识接收器中的可见行。

        • (NSArray *)indexPathsForVisibleItems;
        var visible: [AnyObject] = yourCollectionViewname.indexPathsForVisibleItems
        var indexpath: NSIndexPath = (visible[0] as! NSIndexPath)
        

        【讨论】:

          【解决方案4】:

          这会很好用。

          func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
          
              let visibleIndex = Int(targetContentOffset.pointee.x / collectionView.frame.width)
              print(visibleIndex)
          
          }
          

          【讨论】:

            【解决方案5】:

            你可以使用 UICollectionView 的方法:

            let indexPath = NSIndexPath(item: value1, section: value2)
            

            【讨论】:

            • 你的回答与问题完全无关
            猜你喜欢
            • 1970-01-01
            • 2020-03-21
            • 1970-01-01
            • 1970-01-01
            • 2019-10-20
            • 1970-01-01
            • 2019-11-29
            • 1970-01-01
            • 2021-01-03
            相关资源
            最近更新 更多