LoginSignup
4
3

More than 5 years have passed since last update.

(Swift4)UITableViewにてCellを並び替える方法

Last updated at Posted at 2017-12-12

Swift4構文で書かれている記事を見掛けなかったので書かせて頂きました
間違いなどありましたら教えて下さると助かります

//テスト配列
var todoList:[String] = ["todo01","todo02","todo01","todo03","todo04"]

//並び替えが終わったタイミングで呼ばれるメソッド
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    //sourceIndexPath にデータの元の位置、destinationIndexPath に移動先の位置
    //CellValueを取得
    if let targetTitle = todoList?[sourceIndexPath.row]{
        //元の位置のデータを配列から削除
        todoList?.remove(at:sourceIndexPath.row)
        //移動先の位置にデータを配列に挿入
        todoList?.insert(targetTitle, at: destinationIndexPath.row)
    }
}

参考URL
UITableView の編集モードを利用してデータの削除や並び替えを行う

4
3
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3