我已经添加了一个UILongPressGestureRecognizer
到我的UICollectionViewCell
并创建了一个segue,所以通过长按cell
用户跳到ViewController
。发送细胞数据到destinationViewController与prepareForSegue
我已经有一个普通的使用didSelect
方法显示细胞的细节,但次要长按手势segue使用户到一个操作屏幕删除细胞。
不幸的是使用prepareForSegue
何时发送单元格数据到destinationViewController
因为我用手势识别器,当创建我自己的选择,我不能使用标准let indexPaths = self.collectionView.indexPathForSelectedRow()
方法。
那么如何使用gestureRecognizer
将单元格数据发送到destinationViewController
与prepareForSegue
?
if segue.identifier == "cellAction" {
let vc = segue.destinationViewController as! CellActionViewController
//This will cause an 'array index out of range error
let indexPaths = self.collectionView.indexPathsForSelectedItems()
let indexPath = indexPaths[0] as! NSIndexPath
//Here is how I would send the cell data
let selectedCell = Items[indexPath.row]
vc.selectedItem = selectedCell
}
非常感谢你的回复,你对我如何使用这个姿势是正确的。不幸的是,我仍然是一个初学者,并且不能确定你在将单元格发送给单元类时的意思。我已经创建了一个自定义的UICollectionViewCell类。 – RileyDev
在'prepareForSegue'中,'sender'的类型是'AnyObject?',因为它可以是任何类或'nil'的对象。您需要使用'as?'运算符的可选绑定将其下传到'MyCell'(或者您的任何单元子类被调用)。这是最后一个代码块中的这一行:'if let cell = sender as? MyCell'。您需要将它转换为您的单元类,以便编译器知道您将'MyCell'实例传递给'indexPathForCell(_ :)',而不是'AnyObject?',这会导致编译器错误。 – Stuart
好吧我明白,谢谢你的帮助,我真的很感激它。我喜欢了解代码而不是一味地复制粘贴和回答(Y) – RileyDev