ios - 通过自定义 UITableViewCell 上的 UISwipeGestureRecognizer 更新 CoreData?

标签 ios swift uitableview uiswipegesturerecognizer indexpath

我在 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??
    }

UITableViewDelegatecellForRowAt 中,我声明了滑动操作,并将滑动单元格的 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/

相关文章:

ios - 如何在 iOS 中实现具有自定义 View 的无限列表作为行?

iOS 9 WKWebView 产生 "failed to load resource: cancelled"而 iOS 8 没有

ios - 如何从 Objective-C 调用 C 函数

ios - 为什么在调用 `privateManagedObjectContext.perform` 时会发生崩溃(从 com.apple.main-thread(线程 1)排队)?

ios - 来自 Xib 的 UITableViewCell,单元格高度和单元格选择区域

ios - 如何从外部访问for循环并使其 swift 停止

ios - 如何使用 Xcode 7 UI 测试检查表格 View 中的单元格是否可见?

ios - 根据他子tableview在SWIFT中的高度调整scrollview的高度

ios - Swift:如何将单元格放在一起并根据文本量调整 TableView 的单元格宽度

ios - 尝试对 UITableView 中的单元格重新排序时获取 '-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object'