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()
}
請參閱編輯的問題 – Coder221
@ Coder221你這是什麼想要在'didSelectRowAt:'中填寫'fromIndexPath'和'toIndexPath'? – kennytm
我喜歡填充指數路徑 – Coder221