LoginSignup
7

More than 5 years have passed since last update.

swift TabieViewにスワイプ削除を追加&文言変更

Posted at

スワイプ削除機能を追加

HistoryViewController.swift

    // スワイプ削除
    func tableView(tableView: UITableView,canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool
    {
        return true
    }

Deleteとでる部分のメッセージを変更する場合。

HistoryViewController.swift
    override func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? {
        return "削除"
    }

image

スワイプ削除時の処理

HistoryViewController.swift
    // 削除処理
    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == UITableViewCellEditingStyle.Delete {


            // これはRealmSwiftでデータを削除しているケース
            let deleteHistory = self.result![indexPath.row]
            // トランザクションを開始してオブジェクトを削除します
            try! realm!.write {
                realm!.delete(deleteHistory)
            }


            // TableViewを再読み込み.
            self.table.reloadData()


        }
    }

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
7