Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

385
Views
How to update the tableview row after swipe action?

I am using swipe actions in my tableview. I want to add and delete small icons on the row after a swipe action completed.

I use an asynchronous thread to do this but, it is not giving a smooth result as I wanted. My code is given below any help will be appriciated.

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let groceryAction = groceryToggleAction(forRowAtIndexPath: indexPath)
    let config = UISwipeActionsConfiguration(actions: [groceryAction])
    return config
}

func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let consumeAction = consumeToggleAction(forRowAtIndexPath: indexPath)
    let config = UISwipeActionsConfiguration(actions: [consumeAction])
    return config
}

// MARK: Custom Methods
func groceryToggleAction(forRowAtIndexPath indexPath: IndexPath) -> UIContextualAction {
    let food = foods[indexPath.item]
    let action = UIContextualAction(style: .normal, title: "actionTitle") { (action, view, completionHandler) in

        let food = self.foods[indexPath.item]

        food.isAddedToGrocery = !food.isAddedToGrocery
        self.persistenceManager.saveContext()
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
            self.homeTableView.reloadRows(at: [indexPath], with: .none)
        }
        completionHandler(true)
    }
    action.image = #imageLiteral(resourceName: "shoppingCart") // İyi bir liste ikonu bul...
    action.backgroundColor = food.isAddedToGrocery ? UIColor.Palette.alizarin : UIColor.Palette.turquoise
    action.title = food.isAddedToGrocery ? "Remove" : "Add"
    return action
}

func consumeToggleAction(forRowAtIndexPath indexPath: IndexPath) -> UIContextualAction {
    let food = foods[indexPath.item]

    let action = UIContextualAction(style: .normal, title: "actionTitle") { (action, view, completionHandler) in

        food.isConsumed = !food.isConsumed
        self.persistenceManager.saveContext()
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
            self.homeTableView.reloadRows(at: [indexPath], with: .none)
        }
        completionHandler(true)

    }
    action.image = #imageLiteral(resourceName: "pacman")
    action.title = food.isConsumed ? "Remove": "Consumed!"
    action.backgroundColor = food.isConsumed ? UIColor.Palette.alizarin : UIColor.Palette.turquoise
    return action
}
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Finally I figured it out. I simply used below function from tableview delegate.

func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?) {
    print("did end editing")
    guard let indexPath = indexPath else {return}
    tableView.reloadRows(at: [indexPath], with: .none)
}
over 4 years ago · Santiago Trujillo Report

0

You need to integrate the UITableView delete rows, this will give that nice anim instead of the clunky reload view update.

tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
over 4 years ago · Santiago Trujillo Report

0

DispatchQueue.main.async
{
    self.tableView.reloadData()
}

should be just enough.

In your code, you force the app to execute at a specific time. If your data is dynamic, such as remote images, they could delay the main loop more than 0.4 seconds and thus cause an even more jerky update and movement.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!