Fernand
Fernand

Reputation: 103

Swift tableview, reloadRows after SwipeAction, unable to swipe again on the same row

I have a tableView with swipeActions, and these action call tableView.reloadRows to update my table.

I noticed that i cannot swipe again a just updated row after action.

the problem was this cell keep stats of that SwipeActions panel is shown. I think a simple method that "Swipe back" this cell back will resolve my issue, but i dont find any.

(i dont want to reload whole tableView)

func tableView(_ tableView: UITableView, 
    trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
     let removeAction = UIContextualAction(style: .normal, title: nil, handler: { _, _, _ in
        self.tableView.reloadRows(at: [indexPath], with: .automatic)
//            self.items.remove(at: indexPath.row)
//            self.tableView.deleteRows(at: [indexPath], with: .automatic)
     })
     removeAction.image = UIImage(named: "ic_garbage")!.scaleImageToFitSize(size: CGSize(width: 32.0, height: 32.0))
     removeAction.backgroundColor = UIColor.lightGray
     let actionModify = UIContextualAction(style: .normal, title: nil, handler: { _, _, _ in
        self.tableView.reloadRows(at: [indexPath], with: .automatic)
//            self.changeQty(indexPath)
     })
     actionModify.image = UIImage(named: "ic_exchange")!.scaleImageToFitSize(size: CGSize(width: 32.0, height: 32.0))
     actionModify.backgroundColor = UIColor.orange

     let toReturn = UISwipeActionsConfiguration(actions: [removeAction, actionModify])
     toReturn.performsFirstActionWithFullSwipe = false
     return toReturn
}

Upvotes: 1

Views: 356

Answers (1)

Rob C
Rob C

Reputation: 5073

You need to be calling the completion handler. Your actions should be defined like this...

let action = UIContextualAction(style: .normal, title: "Hello") { (action, view, completion) in
    self.doSomething()
    completion(true)
}

Upvotes: 2

Related Questions