0

I have a piece of code here, which seems to be working just fine apart from one little piece.

import UIKit

class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet var tblTasks: UITableView!

    //Returning to view
    override func viewWillAppear(animated: Bool) {
        tblTasks.reloadData();
    }

    //UItableViewDataSource
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return taskMgr.tasks.count
    }

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

    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if (editingStyle == UITableViewCellEditingStyle.Delete) {
            // handle delete (by removing the data from your array and updating the tableview)
        }
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{        
        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "test")
        cell.textLabel?.text = taskMgr.tasks[indexPath.row].name
        cell.detailTextLabel?.text = taskMgr.tasks[indexPath.row].desc
        return cell
     }
}

I have introduced these lines in order to be able to delete rows in the table view

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

func tableView(tableView: UITableView, 
               commitEditingStyle editingStyle: UITableViewCellEditingStyle, 
               forRowAtIndexPath indexPath: NSIndexPath) {
    if (editingStyle == UITableViewCellEditingStyle.Delete) {
        // handle delete (by removing the data from your array and updating the tableview)            
    }
}

and what they do is giving me the possibility to swipe rows from right to left, though the row doesn't get deleted.

What would be the best possible way to introduce a DELETE button in this case?

best regards, Vlad

1
  • Adding these methods isn't enough to delete rows. You need to remove the item from your datasource and delete the row from the table view as is described in the comment in your commitEditingStyle method. Commented Mar 1, 2015 at 14:01

3 Answers 3

2
override func tableView(tableView: UITableView, commitEditingStyle  editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath:   NSIndexPath) { 

if editingStyle == UITableViewCellEditingStyle.Delete {
//delete row at selected index 
numbers.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) 
              } 
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer. I am now keep getting the error in numbers.removeAtIndex line (I have changed numbers to the name of my array). The error is: use of unresolved identifier (numbers)
1

Here is a cleaned up version for Swift 3.x:

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        // Delete from your source, then update the tableView
        numbers.removeAtIndex(indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .automatic)
    }
}
  1. Check to make sure that the editingStyle is delete before proceeding
  2. Remove the value from your data source (in this case, numbers)
  3. Finally, pass in the indexPath as an array to be deleted, along with the animation you would like (in this case, automatic)

Comments

0
   func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
                if editingStyle == UITableViewCellEditingStyle.Delete { 
   yourArray.removeAtIndex(indexPath.row)

   tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)

    }

 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.