Ios 如何查找选定集合视图单元格的索引路径?

Ios 如何查找选定集合视图单元格的索引路径?,ios,swift,uialertcontroller,nsindexpath,Ios,Swift,Uialertcontroller,Nsindexpath,如何在不使用didSelectItemAtIndexPath和prepareForSegue的情况下查找选定集合视图单元格的索引路径 我正在AlertViewController中使用此indexPath。我不知道如何在AlertView控制器中获取此indexPath //Edit Selected Category let alertController = UIAlertController(title: "Alert", message: "Rename your ",

如何在不使用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委托调用中

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

    return cell
}

在CollectionView委托调用中

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

    return cell
}
使用以下命令:

    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 {
}
使用以下命令:

    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 {
}

若要在不使用didSelect方法的情况下选择单元格,则可以使用点击手势来检测单元格。 选中下面的方法将手势添加到单元格并检测手势

//将以下代码添加到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
}

希望它能起作用。

若要在不使用didSelect方法的情况下选择单元格,则可以使用点击手势来检测单元格。 选中下面的方法将手势添加到单元格并检测手势

//将以下代码添加到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
}

希望它能起作用。

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)
}

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)
}
这是我的解决办法

private func getCurrentIndex() -> Int {
    return Int(collectionView.contentOffset.x / collectionView.frame.width)
}
这是我的解决办法

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


当我选择单元格时,我需要这个indexPath。是的,当你点击单元格时,它会给你一个你点击的特定单元格否@RahulChopra@AvinashMishra请解释这个答案是如何解决问题的(因为它没有)@rmaddy这将为您提供确切的索引路径编号,您将在collectionView单元格中点击该编号,这就是问题所在。根据我的说法,如果还有其他问题,请告诉我。问题是如何获取所选单元格的索引路径。您发布的代码与所选单元格无关。您已将用于为集合视图中的所有项目提供单元格的代码发布到集合视图中。我在选择单元格时需要此indexPath。是,当您点击单元格时,它将为您提供您点击的特定单元格否@RahulChopra@AvinashMishra请解释这个答案是如何解决问题的(因为它没有)@rmaddy这将为您提供确切的索引路径编号,您将在collectionView单元格中点击该编号,这就是问题所在。根据我的说法,如果还有其他问题,请告诉我。问题是如何获取所选单元格的索引路径。您发布的代码与所选单元格无关。您已将用于为所有项目提供单元格的代码发布到集合视图。
collectionView.indexPathsForSelectedItems
如何在没有用户交互的情况下显示此
UIAlertController
?如果您有一些代码,请提供。
collectionView.indexPathsForSelectedItems
如何在没有用户交互的情况下呈现此
UIAlertController
?如果您有一些代码,请提供。我正在使用您的代码,但在打印时没有得到索引路径。@RahulChopra抱歉,遗漏了一件事。您必须在didSelect委托方法中设置选择。有更新应答我正在使用你的代码,但打印时我没有得到索引路径。@RahulChopra抱歉遗漏了一件事。您必须在didSelect委托方法中设置选择。有最新答案吗