Swipe To Delete TableView Row

后端 未结 8 973
暖寄归人
暖寄归人 2020-12-24 06:10

I have my array:

self.colorNames = [[NSArray alloc] 
initWithObjects:@\"Red\", @\"Green\",
@\"Blue\", @\"Indigo\", @\"Violet\", nil];

I\'ve

8条回答
  •  执笔经年
    2020-12-24 06:53

    Swift 3 and Swift 4 answer without using reloadData

    I prefer using deleteRows instead of using reloadData.

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Enables editing only for the selected table view, if you have multiple table views
        return tableView == yourTableView
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Deleting new item in the table
            yourTableView.beginUpdates()
            yourDataArray.remove(at: indexPath.row)
            yourTableView.deleteRows(at: [indexPath], with: .automatic)
            yourTableView.endUpdates()
        }
    }
    

提交回复
热议问题