var longPressTarget: (cell: UICollectionViewCell, indexPath: NSIndexPath)?
func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath){
longPressTarget = (cell: self.collectionView(collectionView, cellForItemAtIndexPath: indexPath), indexPath: indexPath)
}
func longPressHandler(recognizer: UILongPressGestureRecognizer) {
if recognizer.state == UIGestureRecognizerState.Began {
if let _longPressTarget = longPressTarget {
let entity = coin[_longPressTarget.indexPath.item]
let contextMenuController = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
let deleteAction = UIAlertAction(title: entity.pendingDelete ? "Undelete" : "Delete", style: UIAlertActionStyle.Default, handler: togglePendingDelete)
}
}
}
错误发生在:let entity = coin[_longPressTarget.indexPath.item]
硬币:
var coin: [Receipts] = [Receipts] ()
在viewDidLoad
之前调用-coin
收据:核心数据中的实体
我不明白为什么硬币阵列超出范围?
我正在尝试使用长按手势识别器删除UICollectionViewCell
核心数据
答案 0 :(得分:0)
让我们按部分修复您的代码,首先您不要访问调用方法indexPath
的某些cellForItemAtIndexPath:
的单元格。您可以像这样使用方法cellForItemAtIndexPath(_:)
:
func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath) {
longPressTarget = (cell: self.collectionView?.cellForItemAtIndexPath(indexPath), indexPath: indexPath)
}
然而在你的情况下,我不知道你需要什么细胞但是......
在您的其他方法中,您需要使用indexPath.row
而不是indexPath.item
,有不同之处。所以你的代码应该是这样的:
func longPressHandler(recognizer: UILongPressGestureRecognizer) {
if recognizer.state == UIGestureRecognizerState.Began {
if let _longPressTarget = longPressTarget {
let indexPath = _longPressTarget.indexPath
let entity = coin[indexPath.row]
let contextMenuController = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
let deleteAction = UIAlertAction(title: entity.pendingDelete ? "Undelete" : "Delete", style: UIAlertActionStyle.Default, handler: togglePendingDelete)
}
}
}
我希望这对你有所帮助。