Beginning iOS Programming with Swift and UIKit (iOS 15) by Simon Ng

Beginning iOS Programming with Swift and UIKit (iOS 15) by Simon Ng

Author:Simon Ng
Language: eng
Format: epub
Publisher: GitBook


Now open RestaurantDetailViewController.swift and update the total number of cells:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 3 }

Also, update the tableView(_:cellForRowAt:) method like this:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { switch indexPath.row { case 0: let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: RestaurantDetailTextCell.self), for: indexPath) as! RestaurantDetailTextCell cell.descriptionLabel.text = restaurant.description return cell case 1: let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: RestaurantDetailTwoColumnCell.self), for: indexPath) as! RestaurantDetailTwoColumnCell cell.column1TitleLabel.text = "Address" cell.column1TextLabel.text = restaurant.location cell.column2TitleLabel.text = "Phone" cell.column2TextLabel.text = restaurant.phone return cell case 2: let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: RestaurantDetailMapCell.self), for: indexPath) as! RestaurantDetailMapCell return cell default: fatalError("Failed to instantiate the table view cell for detail view controller") } }

In the code above, we just added a new case to handle the map cell. Let's hit the Run button to have a quick test. When you select a restaurant to navigate to the detail view, it shows a map. This is the power of MapKit. Without writing a line of code for the map view, you already embed a map within your app. Right now, it only displays a default map based on your current location. But it's really cool, right?



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.