Tableview Click Edit Mode Delete Swipe Again

iOS

Delete a Row from UITableView and Model-View-Controller


As said in the previous mail, I take another question to answer before moving onto Storyboard tutorial.

How can I delete a row from UITableView?

This is some other mutual question people raised when edifice the Simple Tabular array App. Again, information technology'south easier than y'all thought. Only before jumping into the coding part, I have to introduce you the Model-View-Controller model, which is ane of the near quoted design patterns for user interface programming.

You tin't escape from learning Model-View-Controller (MVC for short) if you lot're serious nearly iOS programming. Not express to iOS programming, MVC is unremarkably used and quoted in other programming languages such as Java. If yous come from other programming backgrounds, MVC shouldn't be new to yous.

Delete Row Featured Image

Understanding Model-View-Controller

At the centre of MVC, and the thought that was the nearly influential to after frameworks, is what I telephone call Separated Presentation. The thought behind Separated Presentation is to make a clear division between domain objects that model our perception of the real world, and presentation objects that are the GUI elements nosotros see on the screen. Domain objects should exist completely self contained and work without reference to the presentation, they should likewise exist able to support multiple presentations, possibly simultaneously. This approach was also an important function of the Unix culture, and continues today allowing many applications to be manipulated through both a graphical and command-line interface.

By Martin Fowler

No thing what reckoner language you learn, one important concept that makes you get a ameliorate developer is Separation of Concerns (SoC). The concept is pretty simple. Concerns are the different aspects of software functionality. The concept encourages developers to pause a large characteristic or program into several areas of business that each area has its own responsibility. The consul pattern that is commonly establish in iOS programming nosotros explained in the earlier tutorial is one of the example of SoC.

Here, model-view-controller is another example of SoC. The core idea behind MVC is to clearly carve up user interface into three areas (or groups of objects) that each surface area is responsible for a particular functionality. As the name suggests, MVC breaks an user interface into three parts:

Model – model is responsible for holding the data or any operations on the data. The model can be as simple equally an array object that stores all the table data. Add, edit and delete are examples of the operations. In reality, the operations are usually known every bit business rules.

View – view manages the visual brandish of information. For case, UITableView shows information in a tabular array view format.

Controller – controller is the span between model and view. It translates the user interaction from the view (eastward.g. tap) into appropriate activeness to be performed in the model. For example, user taps a delete push in the view and controller, in plough, triggers a delete operation in the model. After that, information technology also requests the view to refresh itself to reflect the update of the data model.

To better help you sympathize MVC, allow'south apply our Simple Tabular array app equally an example. The app displays a list of recipes in the tabular array view. If you turn the concept into visual representation, hither is how the table data is displayed:

MVC Model for Simple Table

MVC Model Illustrated Using Simple Table as Example

The recipe information that are stored in dissever array objects is the Model. Each tabular array row maps to an element of the recipe arrays. The UITableView object is the View that is the interface to be seen past the user. It's responsible for all the visuals (due east.g. colour of the table rows, font size and type). The TableViewController acts equally the span between the TableView and Recipe information model. When display tabular array data, UITableView actually asks the Controller for the data to display, that in turn, picks the data from the model.

How To Delete a Row from UITableView

I hope yous have a better agreement nigh Model-View-Controller. Now allow's move onto the coding part and see how we tin delete a row from UITableView. To make thing uncomplicated, I'll use the plainly version of Elementary Table app every bit an example.

If yous thoroughly understand the MVC model, you probably have some ideas how to implement row deletion. In that location are three main things nosotros need to do:

1. Write lawmaking to switch to edit mode for row deletion
two. Delete the corresponding tabular array data from the model
iii. Reload the table view in order to reverberate the change of table data

1. Write code to switch to edit mode for row deletion

In iOS app, user normally swipes beyond a row to initiate the delete button. Recalled that we have adopted the UITableViewDataSource protocol, if y'all 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 run across if the method has been implemented. If the method is constitute, the table view will automatically prove the "Delete" button.

But add the following code to your table view app and run your app:

Even the method is empty and doesn't perform anything, you'll see the "Delete" push button when y'all swipe across a row.

Swipe to Delete Table Row

Swipe to Delete a Table Row

2. Delete the corresponding table data from the model

The next matter is to add lawmaking to the method and remove the actual table data. Like other table view methods, it passes the indexPath every bit parameter that tells you the row number for the deletion. And so you tin make employ of this information and remove the corresponding element from the data array.

In the original code of Uncomplicated Table App, we use NSArray to store the tabular array data (which is the model). The problem of NSArray is it'southward not-editable. That is, you can't add/remove its content once the array is initialized. Alternatively, nosotros'll change the NSArray to NSMutableArray, which adds insertion and deletion operations:

In the tableView:commitEditingStyle method, add the following code to remove the bodily information from the array. Your method should look similar this:

The NSMutableArray provides a number of operations for you lot to dispense the content of an assortment. Here we utilize the "removeObjectAtIndex" method to remove a particular item from the array. You can try to run the app and delete a row. Oops! The app doesn't work as expected.

It's non a problems. The app does delete the item from the array. The reason why the deleted item still appears is the view hasn't been refreshed to reverberate the update of the data model.

3. Reload the table view

Therefore, one time the underlying data is removed, we demand to invoke "reloadData" method to request the table View to refresh. Here is the updated lawmaking:

Test Your App and Delete a Row

Try to run your app again and swipe to delete a row. Y'all should be able to delete it.

Simple Table App - Row Deletion

Delete a Table Row in Simple Table App

Equally always, exit me comment to share your experience about the tutorial.

Update: Y'all can now download the source lawmaking of the Xcode project.

moyahicee1968.blogspot.com

Source: https://www.appcoda.com/model-view-controller-delete-table-row-from-uitableview/

0 Response to "Tableview Click Edit Mode Delete Swipe Again"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel