Open menu with table of contents iOS Development - Framework Basics 2
Logo of Stuttgart Media University for light theme Logo of Stuttgart Media University for dark theme
Mobile Application Development 2

iOS Development - Framework Basics 2

Stuttgart Media University

1 Agenda

  • More UIKit
    • UITableViewController
      • UITableViewDelegate Protocol
      • UITableViewDataSource Protocol
    • UITableView
    • UITableViewCell

2 UITableViewController

  • UIKit class to create lists
    • Similar to ListActivity on Android
  • Contains a UITableView to show items in a list
    • Similar to the ListView on Android
  • Makes use of the Delegate pattern by implementing, e.g. following Protocols + UITableViewDelegate + UITableViewDataSource + Similiar to the ListAdapter on Android

50%

3 UITableViewDelegate

  • The UITableViewDelegate Protocol defines methods that are to be implemented in order to for example:
    • Manage and handle selections
    • Configure Rows, e.g. the height of a row
    • Modifying Header and Footer of Sections
    • Managing TableView highlighting
    • Editing, Copy/Paste and reordering TableRows

4 UITableViewDelegate

  • Example:
    • To handle row selection, override the following method of the UITableViewDelegate Protocol in the UITableViewController:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("Selected row \(indexPath.item)")
}
  • Parameters:
    • tableView: the selected TableView (relevant only, if multiple Tables are handled by this class)
    • indexPath: the index of the selected row. The item property contains an int with the row number

5 UITableViewDataSource

  • The UITableViewDataSource Protocol mediates between the Data Model and the TableView
  • Works similar to the ListAdapter on Android and defines a number of methods that the UITableViewController implements in order to fill the Table with content, e.g. methods to
    • Get the number of sections in a table
    • Get the number of rows in a section
    • Get the cell for a certain row

6 UITableViewDataSource

  • Example methods implemented in the UITableViewController:

// Override to define the number of sections for the table
override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

// Override to define the number of rows in each section
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return shoppingList.count
}

// Override to define the cell for a given row
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)

    // Configure the cell...
   var config = cell.defaultContentConfiguration()
        config.text = shoppingList[indexPath.item]
        config.image = UIImage(systemName: "applelogo")
        config.secondaryText = "Edeka"
        
        cell.contentConfiguration = config

//old way - deprecated
    //cell.textLabel?.text = shoppingList[indexPath.item]

    return cell
}

7 UITableView

  • The UITableView defines the visible list that is controlled by the UITableViewController
  • Inherits from UIScrollView
  • Can have one of two styles
    • UITableViewStylePlain
    • UITableViewStyleGrouped
  • Each row in the UITableView is defined by a UITableViewCell

50%

8 UITableViewCell

  • The UITableViewCell defines the content of the cells that appear in a UITableView

  • Inherits from UIView

  • Has a predefined textLabel, a detailTextLabel and an imageView Property

  • Has a „Reuse Identifier“ to save resources when rendering

  • Supports different Cell-Styles

    • Basic
    • Right Detail
    • Left Detail
    • Subtitle

50%

9 References