Chapter 10 Editing UITableView

Editing Mode

UITableView has an editing property, when set to true UITableView enters editing mode. Editing mode does not allow user to edit content of row.

Header View appears in the top of the table. Header is used to describe table headers and section headers.

In Homepwner, add the following to ItemsViewController.swift:


  • In Main.storyboard, drag a View to the top of the tableView and resize the height to be 60 pts.
  • Drag two buttons into the header view.
  • Select both buttons and open Auto Layout align and select Vertically in Container.
  • Update frames.
  • Open pin menu and configure as shown below


  • Connect buttons to ItemsViewContoller, select toggleEditingMode for edit and addNewItem for add

Add the following to toggleEditingMode button:


Adding Rows

Most common interfaces for adding rows at runtime:

  • A button above cells of the table view
  • A cell with a green plus sign

Add the following to addNewItem:


DataSource of the UITableView determines the number of rows the table view will display.

The following is snippet of code allows for a row to be added to the list.


Since you are now able to add Items to to the ItemStore, the initializer that adds 5 items to the list should be removed from the ItemStore leaving the ItemStore as shown below.


Deleting Rows

Adding functionality to the red circles on each row. When deleting a cell you must do two things remove the row from UITableView and remove the item from the ItemStore.

To do this ItemStore must be able to delete an item, add the following to functions to ItemStore.swift


Also add the following to ItemsViewController.swift.


Moving Rows

To be able to change the order of the items in the list a method to do so must be added to ItemStore.swift.


As well as ItemsViewController.swift


Display User Alerts

Add the following to ItemsViewController.swift, this will create a user warning using the .ActionSheet style which allows the user to deny or back away from the alert without making a decision.

There are two types of styles for alerts:

  • UIAlertControllerStyle.ActionSheet – used to present the user with a list of actions from which to choose from. This method is used if a user can back out of a decision or the action is not critical.
  • UIAlertControllerStyle.Action – used to display critical information o require the user to decide how to proceed.


Modal View Controller – takes over the screen until it has finished.

Design Pattern – solves common software engineering problems. Contains ideas and/or approaches to use in the application.

Types of Design Patterns:

  • Delegation – one objects gives certain responsibilities to another object.
  • Data Source – is responsible for providing data to another object when requested.
  • Model-View-Controller – each object fulfills a role:
    • model – data
    • view – displays the user interface
    • controller – ties the model and view together
  • Target-action pairs – one objects calls a method on another object

Download the PDF file .