Jess Murray
Jess Murray

Reputation: 1339

UITableView Deleting Row. Swift 2

I have a table view which is been created programatically based on an array. I have enabled the editing button on the button item and I want to be able to delete the value on the row from the array which, as a result is removed from the table view.

I have the following code:

class TableViewController: UITableViewController {


var data = ["Apple", "Apricot", "Banana", "Blueberry", "Cantaloupe", "Cherry",
    "Clementine", "Coconut", "Cranberry", "Fig", "Grape", "Grapefruit",
    "Kiwi fruit", "Lemon", "Lime", "Lychee", "Mandarine", "Mango",
    "Melon", "Nectarine", "Olive", "Orange", "Papaya", "Peach",
    "Pear", "Pineapple", "Raspberry", "Strawberry"]


override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 3
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return data.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("test", forIndexPath: indexPath)

    cell.textLabel?.text = data[indexPath.row]
    return cell
}



// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // Return false if you do not want the specified item to be editable.
    return true
}



// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {


        data.removeAtIndex(indexPath.row)

        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    } else if editingStyle == .Insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }    
}
}

when I run this, and click the delete button on a row I get the following error:

malloc: error for object 0x7ffc69f4d580: incorrect checksum for freed object - object was probably modified after being freed. set a breakpoint in malloc_error_break to debug

Any help would be greatly appreciated :)

Upvotes: 3

Views: 410

Answers (2)

Thomas
Thomas

Reputation: 1319

this is because you return 3 in your numberOfSectionsInTableView function...

Try to return just 1:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1 // here
}

Upvotes: 3

Kemal Can Kaynak
Kemal Can Kaynak

Reputation: 1660

You declared 3 sections but using just 1 and after delete operation, your tableview is confusing.

Upvotes: 0

Related Questions