如何在不使用 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/