首页 > 解决方案 > 无法对 tableView 使用 moveRowAtIndexPath

问题描述

目前我在我的自定义表格视图中显示一个简单的对象数组,我还使用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

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

标签: iosarraysswifttableview

解决方案


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

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

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

推荐阅读