【问题标题】:How to find index Path for a selected Collection View Cell?如何查找选定集合视图单元格的索引路径?
【发布时间】:2018-02-02 05:56:41
【问题描述】:

如何在不使用 didSelectItemAtIndexPath 和 prepareForSegue 的情况下找到选定集合视图单元格的索引路径?

我在 AlertViewController 中使用这个 indexPath。我不知道如何在 AlertView 控制器中获取这个 indexPath。

//Edit Selected Category
        let alertController = UIAlertController(title: "Alert", message: "Rename your ", preferredStyle: .alert)
        let actionOK = UIAlertAction(title: "OK", style: .default)

        alertController.addTextField(configurationHandler: {(textField: UITextField) -> Void in

            if let iP: IndexPath = self.collCategory.indexPath(for: CategoryCollectionCell) {

                print("IP \(iP)")
            }

        })

        alertController.addAction(actionOK)
        self.present(alertController, animated: true, completion: nil)

【问题讨论】:

  • collectionView.indexPathsForSelectedItems
  • 如何在没有用户交互的情况下呈现这个UIAlertController?如果您有一些代码,请提供。

标签: ios swift uialertcontroller nsindexpath


【解决方案1】:

使用这个:

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.selectItem(at: indexPath, animated: false, scrollPosition: .bottom)
}

然后你会从 collectionView.indexPathsForSelectedItems 获取路径

if let collectionView = self.YourCollectionView,
    let indexPath = collectionView.indexPathsForSelectedItems?.first,
    let cell = collectionView.cellForItem(at: indexPath) as? YourCell {
}

【讨论】:

  • 我正在使用你的代码,但我在打印时没有得到索引路径。
  • @RahulChopra 抱歉错过了一件事。您必须在 didSelect 委托方法中设置选择。有更新答案
【解决方案2】:

Swift 4-4.2 - 返回单元格的索引。

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cellIndex = indexPath[1] // try without [1] returns a list.
    print(indexPath[1])
    chapterIndexDelegate.didTapChapterSelection(chapterIndex: test)
}

【讨论】:

    【解决方案3】:

    这是我的解决方案

    private func getCurrentIndex() -> Int {
        return Int(collectionView.contentOffset.x / collectionView.frame.width)
    }
    

    【讨论】:

      【解决方案4】:

      要选择没有 didSelect 方法的单元格,您可以使用 Tap Gesture 来检测单元格。 检查以下方法以向单元格添加手势并检测手势。

      //在collectionview单元格返回方法中添加以下代码

      if let gestures = cell. contentView.gestureRecognizers {
                  gestures.forEach({ (tap) in
                      if tap .isKind(of: UITapGestureRecognizer.self) == true {
                          cell.contentView.removeGestureRecognizer(tap)
                      }
                  })
              }
      let tapRec = UITapGestureRecognizer.init(target: self, action: #selector(didTap(sender:)))
      cell.contentView.isUserInteractionEnabled = true
      cell.contentView.addGestureRecognizer(tapRec)
      

      //创建检测手势识别器的方法

      func didTap(sender: UIGestureRecognizer)
      {
          let cell = sender.view?.superview as! yourCell
      }
      

      希望它会起作用。

      【讨论】:

        【解决方案5】:

        在您的 CollectionView 委托调用中

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) 
            print(" Row : \(indexPath.row)")
        
            return cell
        }
        

        【讨论】:

        • 当我选择单元格时我想要这个 indexPath。
        • 是的,当你点击单元格时,它会给你一个特定的单元格,不,你点击@RahulChopra
        • @AvinashMishra 请解释这个答案如何解决所提出的问题(因为它没有)。
        • @rmaddy 这将为您提供一个确切的索引路径编号,您将在其中点击 collectionView 单元格,这就是要问的问题。根据我的说法,如果还有其他事情请告诉我。
        • 问题是问如何获取选中单元格的索引路径。您发布的代码与选定的单元格无关。您将用于为所有项目提供单元格的代码发布到集合视图。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多