ios - 如何找到选定的 Collection View Cell 的索引路径?

标签 ios swift uialertcontroller nsindexpath

如何在不使用 didSelectItemAtIndexPath 和 prepareForSegue 的情况下为选定的 Collection View Cell 查找索引路径?

我在 AlertViewController 中使用这个 indexPath。我不知道如何在 AlertView Controller 中获取此 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)

最佳答案

使用这个:

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

关于ios - 如何找到选定的 Collection View Cell 的索引路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48576528/

相关文章:

ios - 如何在 UITableViewCell 中有两个单独的 detailTextLabel 值?

ios - 在 RxSwift-MVVM 架构中,触发弹出窗口和指示器等 UI 元素的最佳方式是什么?

swift - 尝试在 Swift 5 中设置 UISearchController 的背景颜色

即使勾选了横向模式,ios 模拟器也不会保持水平状态

ios - 使 UIAlertViewController Action 不可点击?

ios - 在应用程序委托(delegate)中显示警报的方法失败,因为它的 rootViewController 不在 View 层次结构中

ios - 为什么 UIAlertController 会用 self 创建一个保留周期?

ios - 带圆点的动画线图

ios - 将 UIIImage 传递给 UIImagePickerController 进行编辑

swift - 如何将此函数输出为一个简单的字符串?