【问题标题】:Updating CoreData through UISwipeGestureRecognizer on custom UITableViewCell?通过自定义 UITableViewCell 上的 UISwipeGestureRecognizer 更新 CoreData?
【发布时间】:2020-04-03 14:32:28
【问题描述】:

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

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

关于如何实现这一点以及如何在单元格上调用单元格动画的任何想法?

【问题讨论】:

  • 我 ... 将滑动单元格的 indexPath 作为参数传递。不,你没有。在目标/动作选择器中传递自定义参数是不可能的。在单元格中声明手势并添加一个回调闭包,它将索引路径传递给单元格本身。不过你为什么不使用内置的滑动动作呢?
  • @vadian 我没有使用内置的滑动操作,因为当滑动单元格时,我在自定义表格视图单元格中为 UIView 设置动画。

标签: ios swift uitableview uiswipegesturerecognizer indexpath


【解决方案1】:

在这些“#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??
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多