Mobile App Development for iOS
Notes Topics Weekly Resources Graded work Professor Code examplesIn this document, you will learn how to use a table view controller as a “select list”. It will display a list of items that were fetched from the network, and enable the user to select one of the items. Upon selection, the selected item will be passed back to the controller that presented the select list.
This is a modal scene, and therefore will use the well-understood “task” pattern (same as for add, edit, detail).
Here’s a brief video demonstration, for Safari browser users:
Why are we doing a select list this way? We already have a picker view, which can be configured to work in the same way. Well, for lists with many items, the picker view UI is frustrating to some users. It has a few other drawbacks when compared to a full-screen table view as well.
In our app, we need a way to select an item. What item? Something from a list of search results.
This is a common task in apps. All students will be familar with the use of select
and option
elements in HTML, and how they are used to make a list of items. On a web page, we typically see this kind of feature as a “list box” or a “dropdown list”, as shown in the web page example, below left.
A similar example has been prepared for a list (table view controller) scene in an iOS app, as shown below right.
How can we do this for our app? There are several ideas involved.
The W11a1Select
code example in the course’s code repository has the essential info needed to understand and use an item-selection list.
As implied above, we suggest that you first implement the item-selection controller with in-memory data items. (For example, create an array of strings in the controller’s viewDidLoad()
method.) When a user taps a row, pass the data back to the presenting controller (in the delegate method call).
How do we handle #5 above (row tap)? We implement another table view delegate method, tableView(_:didSelectRowAt:)
. You have seen this before in one of the earlier code examples. Here is the logic for the method:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// When the row is tapped, we have the index path value
// Use it to extract the data-item from the data source
// (which is the in-memory array of items)
// Call the delegate's searchTask(_:didSelect:) method,
// passing the data-item as a parameter
}
How do we handle #6 above (do something with the passed-back data)? Well, in the presenting controller, conform to the select list controller protocol, and implement the searchTask(_:didSelect:)
delegate method (and the cancel method). The type of the passed-back data is a string.
We do not often use a programmatically-generated or statically-coded list of items as the data source. Instead, we do a query of some kind, from a web API or from a Core Data store.
The sample code above doesn’t change much. The only real difference is how the list items are fetched in the viewDidLoad()
method.
Want to select and pass back multiple items?
Yes, it can be done.
Change the logic:
Need to know a bit more about an item on the list?
Yes, it can be done.
This would be useful in a scenario where the text (and perhaps image) wasn’t enough to resolve ambiguity, and you wanted the user to be able to drill down to more info before committing to their selection.
Here’s how to think about this: