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:

screen-shot-2016-10-21-at-12-28-03-pm

  • 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

screen-shot-2016-10-21-at-12-36-49-pm

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

Add the following to toggleEditingMode button:

screen-shot-2016-10-21-at-12-45-05-pm


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:

screen-shot-2016-10-21-at-12-51-58-pm

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.

screen-shot-2016-11-11-at-12-04-29-am

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.

screen-shot-2016-11-11-at-12-05-23-am


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

screen-shot-2016-11-11-at-12-57-20-am

Also add the following to ItemsViewController.swift.

screen-shot-2016-11-11-at-1-10-37-am


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.

screen-shot-2016-11-11-at-1-17-06-am

As well as ItemsViewController.swift

screen-shot-2016-11-11-at-1-20-17-am


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.

screen-shot-2016-11-11-at-3-14-45-am

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 .

css.php