0

Is there a way that you can call

func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to toIndexPath: IndexPath) {

   let affectedEvent = arrayMoved[fromIndexPath.row]
        arrayMoved.remove(at: fromIndexPath.row)
        arrayMoved.insert(affectedEvent, at: toIndexPath.row) }

in

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){}
Coder221
  • 1,403
  • 2
  • 19
  • 35

1 Answers1

0

tableView(_:moveRowAt:to:) is a delegate method, meaning it is a method that you should implement and let the system call you, not the other way around — normally you should not call a delegate method yourself.

If you want to tell the system to move a row, simply call moveRow(at:to:) of the table view, e.g.

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

After communicating with OP, want OP actually wants to reorder the model to push the selected item to the end. The typical way to do this is something like:

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()
}

or, if you'd like to manually keep the view and model in sync:

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()
}
Community
  • 1
  • 1
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005