How to refresh tableview after deleting row in swift?

update uitableview without reloaddata
refresh tableview swift
clear tableview swift

I have three view controllers.

VC1 and VC3 are UITableViewControllers subclasses, VC2 is UIViewController subclass.

VC1 pass some data to VC2. VC2 save data using userdefaults to an array. Then VC2 pass this data using segue to VC3. VC3 shows data in a table view.

I've added a swipe gesture to delete data from table view at VC3. And it works good. But when I go back (VC3 -> VC2) and again come back to (VC2 -> VC3) deleted row still exists with data. But when i go to VC3 -> VC2 -> VC1 and back again VC1 -> VC2 -> VC3 I found that deleted row actually deleted. I want when VC3 > VC2 then VC2 > VC3 there will be no deleted data exist.

All view controllers are in UINavigationController stack. Code is written with Swift 2.

Why dont you write a function for all views with corresponding tables.

it's like

import Foundation import UIKit

class DeleteRows{
    class func removeRowAtIndexPath(indexPathRow: Int, indexPathSec: Int, table: UITableView) -> UITableView{
    let index = NSIndexPath(forItem: indexPathRow, inSection: indexPathSec)
    table.deleteRowsAtIndexPaths([index], withRowAnimation: UITableViewRowAnimation.Left)

    return table

You can call them by DeleteRows.removeRowAtIndexPath(indexPathRow: 0, indexPathSec: 0,table:Yourtable).delegate = self

How to reload tableview after delete item in cell Swift 2, How can I reload data of a TableView (not TableViewController) Swift 4, Xcode and post the result of the print when viewDidLoad and when you delete row. Refresh the data table. Perform the following steps to refresh the table of data after deleting a row: In order to execute both the removeEntity() method and a call to the table's query method when the removeEntity button is clicked, create a JSF managed bean. Double click the removeEntity() button to invoke the managed bean properties.

Best solution One of the solutions in your case might be to use a Notification pattern.

Send notification from your VC3 class:

NSNotificationCenter.defaultCenter().postNotificationName("ObjectDeleted", object: ["object" : object])

In your VC1 & VC2 you have to subscribe for delete events (somewhere at viewDidLoad())

                                                 selector: #selector(VC1.objectDeleted(_:)),
                                                 name: "ObjectDeleted",
                                                 object: nil)

And you can process it like:

func objectDeleted(notification: NSNotification) {

    guard let object = userInfo["object"] as? YourObjectClass else { return }
    guard let index = dataArray.indexOf(object) else { return }

    tableView.deleteRowsAtIndexPaths([NSIndexPath.init(forRow: index, inSection: 0)], withRowAnimation: .Automatic)

For VC2 you can update your NSUserDefaults container as well in the function above.

Using iOS 8 or earlier you need to unregister for this notification before deallocating the observer object (VC1 or VC2 in your case). If you forget you are at risk of a crash when the notification center sends the next notification to an object that no longer exists. So don'n forget to add a deinit call:

deinit {

How can I reload data of a TableView (not , It's easy to delete rows from a table view, but there is one catch: you need to remove it from the data source first. If you don't do this, iOS will  So please find following codes; I need help to reload/refresh tableview after clos. Reload to refresh your session. global(qos:. Principal of every payment. Check out The Complete iOS Development Course - Build 14 Apps with Swift 2 on Zenva Academy to learn Swift 2 and iOS development from the ground-up with an expert trainer.

You need to delete the data from the datasource and update the tableview with tableview.reloadData()


When you navigate back to VC2(VC3-> VC2) you should bring a reference to what data you deleted so it can be deleted from the dataSource(array, nsuserdefaults). That way when you want to navigate from VC2 -> VC3 you will bring a fresh array from NSUserDefaults and then reload the tableview with that new data.

But I recommend you to make this simpler for you. It seems like you are making trouble for yourself. If you want to access an array with data from multiple viewControllers you should make a new file with a class that holds the array. Take a look at this sudo code:


Class DataSource{
    var arrayWithData = [String]()

         let userDefault = NSUserDefaults.standardUserDefaults()
         self.arrayWithData = userDefaults.arrayForKey("arrayData")

     func deleteData(){
         //Edit the self.arrayWithData and then run this method
         let userDefault = NSUserDefaults.standardUserDefaults()
         userDefault.updateValue(self.arrayWithData, forKey "arrayData")


How to remove cells from a UITableView, You need to delete the data from the datasource and update the tableview with tableview.reloadData() EDIT!! When you navigate back to VC2(VC3-> VC2) you  To receive the latest developer news, visit and subscribe to our News and Updates.

You will need to update the data after deletion.

In viewDidLoad() or at the end of delete method, add:


This should help update the view(s).

To store/persist data, use UserDefaults.

func storeData(name: String) {

    let defaults = UserDefaults.standard
    defaults.set(name, forKey: "Name")
    defaults.synchronize() // Save Data


func getData() -> String {

    if let name = UserDefaults.standard.object(forKey: "Name") {
        return name as! String
    } else {
        return "" // error in retrieving data (ex: name not found)


How to refresh tableview after deleting row in swift?, In this tutorial we will delete a row from a Table View using the "swipe-to-delete" gesture. This tutorial is made with Xcode 10 and built for iOS 12. When the initial View Controller is deleted there isn't a starting point defined. an Table View Controller we also need to update the class declaration line in. Let’s see how we can create a simple Pull-to-Refresh in a UITableView that’s present inside a UIViewController. Pull-to-Refresh is implemented on the UIRefreshControl Class in Swift. Let’s

First thing is to delete the data from your data source that populates your tableView. Most likely you have an array of some class/struct type such as let usersArray = [Users]() Then you need to delete the row from the tableView. Note!!! If you use reload tableView.reloadRows(at: [indexPath], with: .automatic) the app will crash. This methods acts as if you are inserting a row, but your data source is telling there is no data for that specific index, hence the reason why you are should use table.deleteRowsAtIndexPaths([index], withRowAnimation: UITableViewRowAnimation.Left)

//remove users from dataSource
 //delete the row of the tableView
  table.deleteRowsAtIndexPaths([index], withRowAnimation: UITableViewRowAnimation.Left)

Delete Rows from Table View iOS Tutorial, I have a table view where some information is displayed in the subtitle of each cell. After a cell is deleted, this information needs to be  Use other methods of this protocol to enable specific features for your table. For example, you must implement the table View(_: commit: for Row At:) method to enable the swipe-to-delete feature for rows. For information about how to create and configure your table's cells using your data source object, see Filling a Table with Data.

iOS Reload table view data after deleting row, When you reload the data, some of the items in your collection may not change, or the collection… reloadData() on the tableView will update the tableView instantly the scroll position will be The cell items inside the section are also sorted. It will determine if we need to delete, insert or update the item. How can I delete a row from UITableView? In iOS app, user normally swipes across a row to initiate the delete button. Recalled that we have adopted the UITableViewDataSource protocol, if you refer to the API doc, there is a method named tableView:commitEditingStyle:forRowAtIndexPath. When user swipes across a row, the table view will check to see if the method …

iOS: Animate TableView Updates - Stan Ostrovskiy, I think there isn't an iOS developer who hasn't seen this crash before. Finally, after more than ten years, Apple decided to give a decent “attempt to insert row 144 into section 0, but there are only 0 rows in section 0 after the update”. They can also move, delete, and reload items in your data source. Swipe a table view row and have the Delete button appear. Press the delete button, which triggers a confirmation: “Do you really want to delete this?” Based on the user’s response to the confirmation, delete the object from the data source and remove it from the table view, or cancel. The confirmation step is atypical for iOS it seems.

How To Never Accidentally Crash a UITableView Again, ios uitableview delete multiple rows swift refresh tableView after deleting cell, Remove your object from posts array and then reload your tableView in main  The new row shows and if the primary key is a visible column the value for the primary key would show up as a negative number unless you have written and executed a secondary SQL statement (SELECT @@IDENTITY) in RowUpdated event to get the new key for the new row then set the new records primary key to that value.

  • No, this isn't the best approach. As the VCs are related using delegate, closure callbacks or unwinding are better techniques.
  • @vikingosegundo I'd prefer to keep code loosely coupled. Your approach is also fine but until you want to do some architectural changes in your app. Or react on deletion in another place, as it usually happens
  • @vikingosegundo and btw I can't see that any of VCs relates to each other with any protocol
  • Your code isn't lousily coupled – in contrast: it has a hidden dependency to NSNotificationCenter. The VCs relates to each other as they create each other.
  • @vikingosegundo "lousily coupled"? I love it.
  • the question was how to keep data consistent between 3 view controllers
  • reloadData() fully clears the table and reloads data. So if you scrolled down the table and called this method, scroll position is resetted
  • I have added tableview.reloadData to viewDidLoad and viewWillAppear of VC3.But it not works.
  • @Alex and it shouldn't - because even if you update your data container in VC3, it will be overwritten with old one passed from VC2 when you perform your VC3->VC2->VC3 transition
  • @slxl you are right.But i cannot understand your code. Would you please explain or simplify your code.