LoginSignup
61
43

More than 5 years have passed since last update.

【Swift3】UITableViewのセルをスワイプで削除する

Last updated at Posted at 2016-09-28

デモ

delete.gif

デフォルト

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        array.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .fade)
    }
}

ボタンの文字や背景色を変えたい場合

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    let deleteButton: UITableViewRowAction = UITableViewRowAction(style: .normal, title: "削除") { (action, index) -> Void in
        self.array.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .fade)
    }
    deleteButton.backgroundColor = UIColor.red

    return [deleteButton]
}

関連記事

【iOS11】UITableViewセルのスワイプで削除を無効にする方法【Swift】

61
43
0

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
61
43