【Swift4】UITableViewで常時並べ替え可能にする。

参考サイト
UITableViewで常にCellの並び替え(ソート)ができるようにする – Qiita
UITableViewのデリゲートメソッドまとめ – Qiita

viewDidLoadに下記を追加して常時編集状態にする。
tableView.isEditing = true
tableView.allowsSelectionDuringEditing = true

並べ替え可能にするメソッド。
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}

並べ替え結果を処理するメソッド。
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
// TODO: 入れ替え時の処理を実装する(データ制御など)
}

編集状態の見た目を編集する。
//左側の+やーを表示
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return .none //表示させない。
}
//編集モード時に左にずれるか。
func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
return false //ずれない。
}

これらを前回の記事
【Swift4】UITableViewの基本のき。 | iPhoneアプリ備忘録
に適用すると。

import UIKit

class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    //あうとれっとせつぞく
    @IBOutlet weak var tableView: UITableView!
    //てきとうなはいれつ
    var items:[String?] = ["Apple","Banana","Orange"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //デリゲートとデータソースの設定。
        tableView.delegate = self
        tableView.dataSource = self
        //常時並べ替え可にする
        tableView.isEditing = true
        tableView.allowsSelectionDuringEditing = true
    }
    
    //セクションの設定。(必須)とりあえず1
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    //セルの個数。(必須)配列のカウントだけ。
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.items.count
    }
    
    //セル本体の設定(必須)
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //セルのオブジェクトを作ります
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath)
        //タグ番号でオブジェクトにアクセスします。
        let label1 = cell.viewWithTag(1) as! UILabel
        label1.text = self.items[indexPath.row]
        
        return cell
    }
    
    //並び替え可能に
    func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    //並べ替えた結果を配列に適用
    func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        // TODO: 入れ替え時の処理を実装する(データ制御など)
        
        if let targetTitle:String = items[sourceIndexPath.row] {
            //元の位置のデータを配列から削除
            items.remove(at:sourceIndexPath.row)
            //移動先の位置にデータを配列に挿入
            items.insert(targetTitle, at: destinationIndexPath.row)
        }
        print(items)
        //テーブルビューをリロードする。
        tableView.reloadData()
    }
    
    //見た目の編集
    //左側の+やーを表示
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        return .none //表示させない。
    }
    //編集モード時に左にずれるか。
    func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
        return false //ずれない。
    }
}

起動すると
Simulator Screen Shot - iPhone X - 2019-03-14 at 10.47.40
この様な感じでいつでも並べ替え可能になる。

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です