Search code examples
swifttableviewswift3ios10

Table View: right to left swipe to delete does not show up - swift 3


I've built a simple toDoList app with Swift 3. Now I want to be able to delete my items from a TableView by swiping from right to left. This code is what I've found. But nothing happens when I swipe to the left.


CODE:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{

    return toDoList.count
}

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {

    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")

    cell.textLabel?.text = toDoList[indexPath.row]

    return cell
}



//
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if (editingStyle == .delete) {
        toDoList.remove(at: indexPath.row)

        UserDefaults.standard.set(toDoList, forKey: "toDoList")
        tableView.reloadData()
    }
}

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
    return .delete
}

This still does not work. Nothing happens when I swipe to the left. The to do List itself is working. I can add items to the table but I just can't remove them.

Thanks :)


Solution

  • Did you implement tableView:canEditRowAtIndexPath: method?

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true  
    }
    

    EDIT:

    Thanks to @rmaddy for mentioning that the default value of tableView:canEditRowAtIndexPath: is true, implementing it doesn't solve the problem.

    I'm not pretty sure of what are you trying to do from your code snippet, so make sure that you are implementing the following methods (UITableViewDelegate):

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if (editingStyle == .delete) {
            toDoList.remove(at: indexPath.row)
    
            UserDefaults.standard.set(toDoList, forKey: "toDoList")
            tableView.reloadData()
        }
    }
    
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
        return .delete
    }
    

    You can also keep the implementation of tableView:canEditRowAtIndexPath: method:

    Asks the data source to verify that the given row is editable.

    So, -for example- if you want to let the first row is not editable, i.e user cannot swipe and delete the first row, you should do somthing like:

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        if indexPath.row == 0 {
            return false
        }
    
        return true
    }
    

    Make sure that the UITableViewDataSource and UITableViewDelegate are connected with the ViewController.