LoginSignup
35
31

More than 5 years have passed since last update.

スワイプでUITableViewCellの削除

Last updated at Posted at 2015-05-20

セルの削除許可を設定

func tableView(tableView: UITableView,canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool
{
return true
}

trueで編集許可。セクションや行で判定して許可を設定する。

セルの削除ボタンが押された時の処理

editingStyleにdeleteが入っていれば、削除ボタンが押されたと分かる。
下の例はセル表示で使っている配列を削除するとともに、見た目上のセルも削除している。

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == UITableViewCellEditingStyle.Delete {
            arr.removeObjectAtIndex(indexPath.row)
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
        }
    }
35
31
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
35
31