Hi @Ammar71.
Please don’t confuse Lua tables with the widget.newTableView() object. Under the hood, widget.newTableView() utilizes at least one Lua table, and the object itself is a Lua table. But this isn’t really relevant to your question.
I would suggest you maybe take some time and do an Internet search on the term MVC or “Model View Controller”. This is a design pattern that the widget.newTableView() is part of. Let me give you a quick primer and then I’ll explain how the widget.newTableView() fits in to this.
In a Model-View-Controller setup, the Model is the data. Typically a database, it’s the rows, columns, schema that defines the underlying data. The “View” is the code that presents the data to the user. The model and the view are supposed to be independent. That is the developers can change the model and it not directly impact the view, while the developers can change the view without ever touching the model. This works because in-between there is a controller that is aware of the of the changes on both ends. The view will ask the controller for a name and the view doesn’t care how it gets a name. The controller knows how to to get the “Name” from the database.
Corona doesn’t exactly implement MVC in this way, mostly because we have zero control over your data. We don’t know what the model is. If fact, I wouldn’t even say we know what the view is, but the widget.newTableView() is just a tool in your control when you go to create your view.
In the simplest of terms, your model will likely be a pure Lua table (not attached to the tableView in any way). You could populate it from an SQLite database, a JSON object, a REST API call using network.request() etc. As long as your data ends up in a Lua table you’re good to go.
Now you have a tableView() and you get your data into the tableView by calling tableView:insertRow(). Now the widget.newTableView() is just a view into your model and the tableView row’s will show the representation of your model’s data.
So now, if you delete a row from the tableView, all you’ve done is removed it from the view. You’ve not removed it from your model/data. If you do remove it from your Lua table/model, you can do the table.remove() option (http://docs.coronalabs.com/api/library/table/remove.html) to remove the record from the table and have the index numbers recalculated. But this is a very computationally expensive operation.
However, if you need the tableView to re-index to match your data, remove it from your data, remove all rows from the tableView and re-insert them should be fast enough (since it only has to update the on screen rows) that you wouldn’t notice the action.
You should never depend on the tableView indexes to represent your data. They are expected to get out of sync. What you should do is store the index of the data in with the tableRow and then use that index to lookup the data to show.
Rob