1/5

iOS Assignment

UITableViewController

Please conduct the following tasks alone. For implementation details you can refer to the lecture slides or the Apple developer website. Please do not hesitate to ask me or the tutor if you have any questions. Under “Tip” you can find some hints for the task. The “Checklist” specifies what should happen when your implementation is correct.

2/5

Tasks

  1. Small ShoppingList-App
  2. Add selection listener to the shopping list
  3. Passing information to the second view
3/5

1. Small ShoppingList-App

Create a new Xcode project with a „Single View Application“-template.

Now add a new UITableViewController subclass to it. Create a String-Array with shopping items as in 02-iOS-Assignment-Swift. The shopping list items should show up in the TableView.

Tip

Do not forget to set the „Reuse Identifier“ in the StoryBoard and to link your newly created class file to the UITableViewController in the StoryBoard.

Checklist
When the app is started all shopping items show up in the TableView
4/5

2. Add selection listener to the shopping list

Embed the TableViewController in a NavigationController. Draw a segue from the UITableViewCell (the prototype cell) to the second View. The result should look similar to this:

center 70%

Tip

Use the „Editor“ menu to embed the TableView in a NavigationController

Checklist
When the app is started the shopping list is in a NavigationController
When tapping on a list item the second view is shown including the back button in the navigation bar
5/5

3. Passing information to the second view

Extend your app so that the shopping item that has been selected in the TableView is shown in the Label of the second view.

center 60%

This requires to link the Label in the StoryBoard to the ViewController of the second view. When the view is loaded, the Label’s text should be set to the value of a new Property called „itemText: String“. Thus, please create this new Property in the ViewController class. Then in the viewDidLoad() method do the following to set the Label’s text:


override func viewDidLoad() {
    super.viewDidLoad()

    itemLabel.text = itemText

}

The Property „itemText“ should be set to the name of the selected item, just before the second view is shown. This is done using the prepareForSegue() method. Please implement this method in your TableViewController class as shown below:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        let cell = sender as? UITableViewCell
        let detailView = segue.destination as? ViewController
        if (detailView != nil) {
            let index = tableView.indexPath(for: cell!)!
            detailView?.itemText = shoppingList[index.row]
        }
        
}

This method is called just before the segue transition is executed. The „segue“ object allows you to get access to the destination View Controller which in this case is the ViewController with the Label. By using the „indexPath(for: cell!)“ method you can find out which row was selected (the „sender“ is the selected TableViewCell). 
Then you can pick the right item from your shopping list (the items Array in this case) and set the text that is subsequently set to the Label when the second view is loaded.
Unfortunately it is not possible to access the Label of the second view directly at this stage, as the Label is lazily initialized and when the view is not visible, it is nil.

Tip

Read the Apple documentation on StoryBoards to get more information

Checklist
When tapping on a list item the second view is shown and the Label is set to the name of the item that was selected