Table Row Deletion, Swipe for Actions, Activity Controller and MVC
If you spend too much time thinking about a thing, you'll never get it done. Make at least one definite move daily toward your goal.
– Bruce Lee
In the earlier chapter, you learned how to handle table row selection. But how about deletion? How can we delete a row from a table view? It's a common question when building a table-based app. Select, delete, insert and update are the basic operations when dealing with data. We've discussed how to select a table row. Let's talk about deletion in this chapter. In addition, we'll go through a couple of new features to the FoodPin app:
- Adding a custom action button when a user swipes horizontally in a table row. This is usually known as Swipe for More action.
- Adding a social sharing feature to the app, that enables users to easily share the restaurants.
There is a lot to learn in this chapter, but it's going to be fun and rewarding. Let's get started.
A Brief Introduction to Model View Controller
We briefly mentioned the term MVC in the previous chapter when organizing the source files. Before jumping into the coding part, I would like to give you an introduction of Model-View-Controller (MVC) model, which is one of the most quoted design patterns for user interface programming.
I try to keep this book as practical as possible and seldom talk about the programming theories. Similar to OOP, if your ultimate goal is to become a professional developer, you can't avoid learning Model-View-Controller. MVC is not a concept that applies to iOS programming only. You may have heard of it if you've studied other programming languages, such as Java or Ruby. It is a powerful design pattern used in designing software applications, whether it is a mobile app and a web app.
At the heart of MVC, and the idea that was the most influential to later frameworks, is what I call Separated Presentation. The idea 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 we see on the screen. Domain objects should be completely self contained and work without reference to the presentation, they should also be able to support multiple presentations, possibly simultaneously. This approach was also an important part 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 matter which programming language you learn, one important concept that you need to know is Separation of Concerns (SoC). The concept is pretty simple. Here, the Concerns are different aspects of software functionality. This concept encourages developers to break a complicated feature or program into several areas of concern such that each area has its own responsibility. The delegate pattern, that we explained in the earlier chapters, is one of the examples of SoC.
The model-view-controller (MVC) concept is another example of SoC. The core idea behind MVC is to separate a user interface into three areas (or groups of objects) that each area is responsible for a particular functionality. As the name suggests, MVC breaks a user interface into three parts:
- Model – a model is responsible for holding the data or any operations on the data. The model can be as simple as an array object that stores the table data. Add, update and delete are examples of the operations. In the business world, these operations are usually known as business rules.
- View – a view manages the visual display of information. For example, UITableView displays data in a list format.
- Controller – a controller is a bridge between the model and the view. It translates the user interaction from the view (e.g. tap) into the appropriate action to be performed in the model. For example, a user taps the delete button in the view. Consequently, the controller triggers a delete operation in the model. Once finished, the model requests the view to refresh itself so as to reflect the update of the data model.
To help you better understand MVC, let's use the FoodPin app as an example. The app displays a list of restaurants in the table view. If you turn the implementation into a visual illustration, here is how the table data is displayed:
To access the full version of the book, please get the full copy here. You will also be able to access the full source code of the project.