我在 UITableViewCell
上添加自定义滑动手势,当滑动特定单元格时,该手势会更新 CoreData
。但是,我在将滑动单元格的 indexPath
传递给将修改 CoreData
的函数时遇到问题。 我没有使用表格 View 滑动操作 - 当单元格被滑动以划掉项目时,我会为单元格设置动画,这意味着我需要在自定义 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??
}
在 UITableViewDelegate
的 cellForRowAt
中,我声明了滑动操作,并将滑动单元格的 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 作为自定义识别器的属性。
使用该索引路径,而不是传入的索引路径。这可能不是正确的实例。
类似的事情可能就是您想要做的。
@objc func handleSwipeGesture(sender: SwipeCompletion) {
itemArray[sender.indexPath.row].complete = !itemArray[sender.indexPath.row].complete
print("\(itemArray[sender.indexPath.row].complete)")
saveItems()
//call animation??
}
关于ios - 通过自定义 UITableViewCell 上的 UISwipeGestureRecognizer 更新 CoreData?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59266655/