【问题标题】:Unable to use moveRowAtIndexPath for tableView无法对 tableView 使用 moveRowAtIndexPath
【发布时间】:2018-10-25 15:16:31
【问题描述】:

目前我在我的自定义表格视图中显示一个简单的对象数组,我还使用SwipyCell 来测试滑动手势。

然后我尝试使用moveRowAtIndexPath 重新排序对象,但是由于某种原因它不适用于以下代码。

func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {

    let itemThatMoved = self.tasks[sourceIndexPath.row]
    self.array.remove(at: sourceIndexPath.row)
    self.array.insert(itemThatMoved, at: destinationIndexPath.row)


     // Change data properties

}

func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // Return NO if you do not want the item to be re-orderable.
    return true
}

我还在 ViewController 中设置了以下内容

table.isUserInteractionEnabled = true
table.allowsSelectionDuringEditing = true
table.allowsSelection = true

有人对我可能做错了什么有任何建议吗?

【问题讨论】:

  • 这真的应该是 Swift 2 吗?如果,我们鼓励您更新。如果 no 代表方法的签名是错误的。
  • @vadian 你能详细说明一下吗?不,它不应该是 Swift 2
  • 你是如何添加这两个委托方法的?您是否从一些示例代码中复制和粘贴?不要复制旧代码,也不要自己输入。让 Xcode 为你输入大部分内容。

标签: ios arrays swift tableview


【解决方案1】:

如果您使用的是 Swift 3+,则委托方法的签名是错误的

func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool

func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)

【讨论】:

  • 那么你的代码又以另一种方式搞砸了。您似乎在 Swift 3+ 环境中复制和粘贴了 Swift 2 代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-26
  • 1970-01-01
  • 1970-01-01
  • 2014-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多