user2585945
user2585945

Reputation: 139

Swift MGSwipeTableCell Swipe to Delete Cell

I am using xCode 7 beta and Swift to implement a tableview with MGSwipeTableCells. I am doing this because I need to have a swipe button on both the left and right of each cell. Both of these buttons needs to remove the cell from the tableview.

I tried doing this by using the convenience callback method when adding the buttons to the cells:

// Layout table view cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier("newsFeedCell", forIndexPath: indexPath) as! NewsFeedCell
    cell.layoutIfNeeded()

    // Add a remove button to the cell
    let removeButton = MGSwipeButton(title: "Remove", backgroundColor: color.removeButtonColor, callback: {
        (sender: MGSwipeTableCell!) -> Bool in

        // FIXME: UPDATE model
        self.numberOfEvents--
        self.tableView.deleteSections(NSIndexSet(index: indexPath.section), withRowAnimation: UITableViewRowAnimation.Fade)
        return true
    })
    cell.leftButtons = [removeButton]

However, once I delete the first cell, all the indices are thrown off and the callback now deletes an incorrect cell. That is, if I delete cell_0, cell_1 now becomes the first 0th in the table. However, the callback for the buttons associated with cell_1 delete the cell with index 1 even though it is actually now the 0th cell in the table.

I tried to implement the MGSwipeTableCell delegate methods, but to no avail. None of these methods were ever called in the execution of my code. How should I fix this problem? Will implementing the delegate solve this issue? If, so would it be possible to provide an example? If not, can you please suggest an alternate way to have tableview cells with swipe buttons on both sides that can delete said cells?

Upvotes: 2

Views: 2425

Answers (2)

JGilly
JGilly

Reputation: 5

Using the delegate methods will allow for cleaner button creation and table cell removal, because the buttons will only be created for the cell when it is swiped (saves memory) and you can capture a weak reference to the 'sender' (an MGTableViewCell, or custom type) in the handler, from which you can then get the index path. Follow their example on Github: MGSwipeTableCell/demo/MailAppDemo/MailAppDemo/MailViewController.m

Then in your cellForRowAtIndexPath, be sure to set the cell's delegate to 'self.' It looks like you're missing this, and it should fix your problem with delegate methods.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let reuseIdentifier = "cell"
    let cell = self.table.dequeueReusableCellWithIdentifier(reuseIdentifier) as! MGSwipeTableCell!
    cell.delegate = self

    // Configure the cell

    return cell
}

Happy coding!

Upvotes: 0

Muhammad Zeeshan
Muhammad Zeeshan

Reputation: 2451

You can also do something like this to get the correct indexPath:

let removeButton = MGSwipeButton(title: "Remove", backgroundColor: color.removeButtonColor, callback: {
            (sender: MGSwipeTableCell!) -> Bool in

  let indexPath = self.tableView.indexPathForCell(sender)         
  self.tableView.deleteSections(NSIndexSet(index: indexPath.section), withRowAnimation: UITableViewRowAnimation.Fade)
            return true
        })

Upvotes: 1

Related Questions