在didSelectRowAt中调用moveRowAt方法?

时间:2017-03-31 15:05:05

标签: swift uitableview

有没有办法可以打电话

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){}

in

{{1}}

1 个答案:

答案 0 :(得分:0)

tableView(_:moveRowAt:to:)是一个delegate method,这意味着它是一个你应该实现的方法,让系统给你打电话,而不是相反 - 通常你不应该自己调用委托方法。

如果您想告诉系统移动一行,只需表格视图的call moveRow(at:to:),例如

func tableView(_ tableView: UITableView, didSelectRowAt ip: IndexPath) {
    tableView.moveRow(at: ip, to: IndexPath(row: 0, section: ip.section))
}

与OP通信后,希望OP实际上想要重新排序模型以将所选项目推送到最后。执行此操作的典型方法如下:

func tableView(_ tableView: UITableView, didSelectRowAt ip: IndexPath) {
    // update the model.
    model[ip.row].value = maxOfValue + 1
    sortModelAgain()

    // reload the model (note: no animation if using this)
    tableView.reloadData()
}

或者,如果您想手动保持视图和模型同步:

func tableView(_ tableView: UITableView, didSelectRowAt ip: IndexPath) {
    // update the model.
    model[ip.row].value = maxOfValue + 1
    sortModelAgain()

    // change the view to keep in sync of data.
    tableView.beginUpdates()
    let endRow = tableView.numberOfRows(inSection: ip.section) - 1
    tableView.moveRow(at: ip, to: IndexPath(row: endRow, section: ip.section))
    tableView.endUpdates()
}