我正在UITableViewCell
上添加自定义的滑动手势,当特定单元格被滑动时,该手势将更新CoreData
。但是,我无法将滑动单元格的indexPath
传递给将修改CoreData
的函数。 [我没有使用表格视图滑动动作-滑动该单元以将其划掉时,我正在对单元进行动画处理,这意味着我将需要在自定义UITableViewCell
上调用动画。到目前为止,我已经尝试将滑动单元格的indexPath传递给UISwipeGestureRecognizer的#selector
,如下所示:
处理手势的功能:
@objc func handleSwipeGesture(sender: UISwipeGestureRecognizer ,at indexPath: IndexPath) {
itemArray[indexPath.row].complete = !itemArray[indexPath.row].complete
print("\(itemArray[indexPath.row].complete)")
saveItems()
// call animation??
}
在cellForRowAt
的UITableViewDelegate
中,我声明了滑动动作,并将滑动单元格的indexPath作为参数传递给上述函数。
let swipeToComplete = SwipeCompletion.init(target: self, action: #selector(handleSwipeGesture))
swipeToComplete.indexPath = indexPath
cell.addGestureRecognizer(swipeToComplete)
这是使我能够通过以下路径传递indexPath的类:
class SwipeCompletion: UISwipeGestureRecognizer {
var indexPath: IndexPath!
}
但是,当我在单元格上滑动时,会出现错误:EXC_BAD_ACCESS (code=1, address=0xf)
关于如何实现此目标以及如何在单元格上调用单元格动画的任何想法?
在这些“ #selectors”上,您无法自定义传递给调用的值(据我所知。)>
似乎您将indexPath作为自定义识别器的属性。
使用该indexPath,而不是传入的那一个。那可能不是正确的实例。
类似的事情可能是您尝试做的。
@objc func handleSwipeGesture(sender: SwipeCompletion) {
itemArray[sender.indexPath.row].complete = !itemArray[sender.indexPath.row].complete
print("\(itemArray[sender.indexPath.row].complete)")
saveItems()
//call animation??
}