开启专案:
开启 RestaurantTableViewController.swift 档案,贴上以下程式码
//
// RestaurantTableViewController.swift
// FoodPin
//
// Created by Simon Ng on 28/10/2019.
// Copyright © 2019 AppCoda. All rights reserved.
//
import UIKit
class RestaurantTableViewController: UITableViewController {
var restaurantNames = ["Cafe Deadend", "Homei", "Teakha", "Cafe Loisl", "Petite Oyster", "For Kee Restaurant", "Po's Atelier", "Bourke Street Bakery", "Haigh's Chocolate", "Palomino Espresso", "Upstate", "Traif", "Graham Avenue Meats", "Waffle & Wolf", "Five Leaves", "Cafe Lore", "Confessional", "Barrafina", "Donostia", "Royal Oak", "CASK Pub and Kitchen"]
var restaurantLocations = ["Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Sydney", "Sydney", "Sydney", "New York", "New York", "New York", "New York", "New York", "New York", "New York", "London", "London", "London", "London"]
var restaurantTypes = ["Coffee & Tea Shop", "Cafe", "Tea House", "Austrian / Causual Drink", "French", "Bakery", "Bakery", "Chocolate", "Cafe", "American / Seafood", "American", "American", "Breakfast & Brunch", "Coffee & Tea", "Coffee & Tea", "Latin American", "Spanish", "Spanish", "Spanish", "British", "Thai"]
var restaurantImages = ["cafedeadend", "homei", "teakha", "cafeloisl", "petiteoyster", "forkeerestaurant", "posatelier", "bourkestreetbakery", "haighschocolate", "palominoespresso", "upstate", "traif", "grahamavenuemeats", "wafflewolf", "fiveleaves", "cafelore", "confessional", "barrafina", "donostia", "royaloak", "caskpubkitchen"]
var restaurantIsVisited = Array(repeating: false, count: 21)
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.cellLayoutMarginsFollowReadableWidth = true
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return restaurantNames.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "datacell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! RestaurantTableViewCell
// Configure the cell...
cell.nameLabel.text = restaurantNames[indexPath.row]
cell.locationLabel.text = restaurantLocations[indexPath.row]
cell.typeLabel.text = restaurantTypes[indexPath.row]
cell.thumbnailImageView.image = UIImage(named: restaurantImages[indexPath.row])
// Solution to exercise 2
cell.heartImageView.isHidden = !self.restaurantIsVisited[indexPath.row]
return cell
}
// MARK: - UITableViewDelegate Methods
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Create an option menu as an action sheet
let optionMenu = UIAlertController(title: nil, message: "What do you want to do?", preferredStyle: .actionSheet)
// For iPad
if let popoverController = optionMenu.popoverPresentationController {
if let cell = tableView.cellForRow(at: indexPath) {
popoverController.sourceView = cell
popoverController.sourceRect = cell.bounds
}
}
// Add actions to the menu
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
optionMenu.addAction(cancelAction)
// Add Call action
let callActionHandler = { (action:UIAlertAction!) -> Void in
let alertMessage = UIAlertController(title: "Service Unavailable", message: "Sorry, the call feature is not available yet. Please retry later.", preferredStyle: .alert)
alertMessage.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alertMessage, animated: true, completion: nil)
}
let callAction = UIAlertAction(title: "Call " + "123-000-\(indexPath.row)", style: .default, handler: callActionHandler)
optionMenu.addAction(callAction)
// Check-in action
let checkInTitle = self.restaurantIsVisited[indexPath.row] ? "Undo Check in" : "Check in"
let checkInAction = UIAlertAction(title: checkInTitle, style: .default, handler: {
(action:UIAlertAction!) -> Void in
let cell = tableView.cellForRow(at: indexPath) as? RestaurantTableViewCell
// Solution to exercise 1
// cell?.accessoryType = self.restaurantIsVisited[indexPath.row] ? .none : .checkmark
// Solution to exercise 2
// We use the isHidden property to control whether the image view is displayed or not
cell?.heartImageView.isHidden = self.restaurantIsVisited[indexPath.row]
self.restaurantIsVisited[indexPath.row] = self.restaurantIsVisited[indexPath.row] ? false : true
})
optionMenu.addAction(checkInAction)
// Display the menu
present(optionMenu, animated: true, completion: nil)
// Deselect a row
tableView.deselectRow(at: indexPath, animated: false)
}
}
向左滑动即有效果
覆蓋以下程式码
//
// RestaurantTableViewController.swift
// FoodPin
//
// Created by Simon Ng on 28/10/2019.
// Copyright © 2019 AppCoda. All rights reserved.
//
import UIKit
class RestaurantTableViewController: UITableViewController {
var restaurantNames = ["Cafe Deadend", "Homei", "Teakha", "Cafe Loisl", "Petite Oyster", "For Kee Restaurant", "Po's Atelier", "Bourke Street Bakery", "Haigh's Chocolate", "Palomino Espresso", "Upstate", "Traif", "Graham Avenue Meats", "Waffle & Wolf", "Five Leaves", "Cafe Lore", "Confessional", "Barrafina", "Donostia", "Royal Oak", "CASK Pub and Kitchen"]
var restaurantLocations = ["Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Sydney", "Sydney", "Sydney", "New York", "New York", "New York", "New York", "New York", "New York", "New York", "London", "London", "London", "London"]
var restaurantTypes = ["Coffee & Tea Shop", "Cafe", "Tea House", "Austrian / Causual Drink", "French", "Bakery", "Bakery", "Chocolate", "Cafe", "American / Seafood", "American", "American", "Breakfast & Brunch", "Coffee & Tea", "Coffee & Tea", "Latin American", "Spanish", "Spanish", "Spanish", "British", "Thai"]
var restaurantImages = ["cafedeadend", "homei", "teakha", "cafeloisl", "petiteoyster", "forkeerestaurant", "posatelier", "bourkestreetbakery", "haighschocolate", "palominoespresso", "upstate", "traif", "grahamavenuemeats", "wafflewolf", "fiveleaves", "cafelore", "confessional", "barrafina", "donostia", "royaloak", "caskpubkitchen"]
var restaurantIsVisited = Array(repeating: false, count: 21)
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{
restaurantNames.remove(at: indexPath.row)
restaurantLocations.remove(at: indexPath.row)
restaurantTypes.remove(at: indexPath.row)
restaurantIsVisited.remove(at: indexPath.row)
restaurantImages.remove(at: indexPath.row)
}
tableView.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.cellLayoutMarginsFollowReadableWidth = true
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return restaurantNames.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "datacell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! RestaurantTableViewCell
// Configure the cell...
cell.nameLabel.text = restaurantNames[indexPath.row]
cell.locationLabel.text = restaurantLocations[indexPath.row]
cell.typeLabel.text = restaurantTypes[indexPath.row]
cell.thumbnailImageView.image = UIImage(named: restaurantImages[indexPath.row])
// Solution to exercise 2
cell.heartImageView.isHidden = !self.restaurantIsVisited[indexPath.row]
return cell
}
// MARK: - UITableViewDelegate Methods
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Create an option menu as an action sheet
let optionMenu = UIAlertController(title: nil, message: "What do you want to do?", preferredStyle: .actionSheet)
// For iPad
if let popoverController = optionMenu.popoverPresentationController {
if let cell = tableView.cellForRow(at: indexPath) {
popoverController.sourceView = cell
popoverController.sourceRect = cell.bounds
}
}
// Add actions to the menu
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
optionMenu.addAction(cancelAction)
// Add Call action
let callActionHandler = { (action:UIAlertAction!) -> Void in
let alertMessage = UIAlertController(title: "Service Unavailable", message: "Sorry, the call feature is not available yet. Please retry later.", preferredStyle: .alert)
alertMessage.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alertMessage, animated: true, completion: nil)
}
let callAction = UIAlertAction(title: "Call " + "123-000-\(indexPath.row)", style: .default, handler: callActionHandler)
optionMenu.addAction(callAction)
// Check-in action
let checkInTitle = self.restaurantIsVisited[indexPath.row] ? "Undo Check in" : "Check in"
let checkInAction = UIAlertAction(title: checkInTitle, style: .default, handler: {
(action:UIAlertAction!) -> Void in
let cell = tableView.cellForRow(at: indexPath) as? RestaurantTableViewCell
// Solution to exercise 1
// cell?.accessoryType = self.restaurantIsVisited[indexPath.row] ? .none : .checkmark
// Solution to exercise 2
// We use the isHidden property to control whether the image view is displayed or not
cell?.heartImageView.isHidden = self.restaurantIsVisited[indexPath.row]
self.restaurantIsVisited[indexPath.row] = self.restaurantIsVisited[indexPath.row] ? false : true
})
optionMenu.addAction(checkInAction)
// Display the menu
present(optionMenu, animated: true, completion: nil)
// Deselect a row
tableView.deselectRow(at: indexPath, animated: false)
}
}
左滑按下删除之后,该项将会不见
将第 31 行改为以下的程式码,即可实现比较好看的移除动画
tableView.deleteRows(at: [indexPath], with: .fade)
覆蓋以下程式码
//
// RestaurantTableViewController.swift
// FoodPin
//
// Created by Simon Ng on 28/10/2019.
// Copyright © 2019 AppCoda. All rights reserved.
//
import UIKit
class RestaurantTableViewController: UITableViewController {
var restaurantNames = ["Cafe Deadend", "Homei", "Teakha", "Cafe Loisl", "Petite Oyster", "For Kee Restaurant", "Po's Atelier", "Bourke Street Bakery", "Haigh's Chocolate", "Palomino Espresso", "Upstate", "Traif", "Graham Avenue Meats", "Waffle & Wolf", "Five Leaves", "Cafe Lore", "Confessional", "Barrafina", "Donostia", "Royal Oak", "CASK Pub and Kitchen"]
var restaurantLocations = ["Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Sydney", "Sydney", "Sydney", "New York", "New York", "New York", "New York", "New York", "New York", "New York", "London", "London", "London", "London"]
var restaurantTypes = ["Coffee & Tea Shop", "Cafe", "Tea House", "Austrian / Causual Drink", "French", "Bakery", "Bakery", "Chocolate", "Cafe", "American / Seafood", "American", "American", "Breakfast & Brunch", "Coffee & Tea", "Coffee & Tea", "Latin American", "Spanish", "Spanish", "Spanish", "British", "Thai"]
var restaurantImages = ["cafedeadend", "homei", "teakha", "cafeloisl", "petiteoyster", "forkeerestaurant", "posatelier", "bourkestreetbakery", "haighschocolate", "palominoespresso", "upstate", "traif", "grahamavenuemeats", "wafflewolf", "fiveleaves", "cafelore", "confessional", "barrafina", "donostia", "royaloak", "caskpubkitchen"]
var restaurantIsVisited = Array(repeating: false, count: 21)
/*override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{
restaurantNames.remove(at: indexPath.row)
restaurantLocations.remove(at: indexPath.row)
restaurantTypes.remove(at: indexPath.row)
restaurantIsVisited.remove(at: indexPath.row)
restaurantImages.remove(at: indexPath.row)
}
//tableView.reloadData()
tableView.deleteRows(at: [indexPath], with: .fade)
}*/
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "Delete"){(action, sourceView, CompletionHandler) in
self.restaurantNames.remove(at: indexPath.row)
self.restaurantLocations.remove(at: indexPath.row)
self.restaurantTypes.remove(at: indexPath.row)
self.restaurantIsVisited.remove(at: indexPath.row)
self.restaurantImages.remove(at: indexPath.row)
CompletionHandler(true)
}
let shareAction = UIContextualAction(style: .normal, title: "Share") {(action, sourceView, CompletionHandler) in
let defaultText = "Just checking in at " + self.restaurantNames[indexPath.row]
let activityController = UIActivityViewController(activityItems: [defaultText], applicationActivities: nil)
self.present(activityController, animated: true, completion: nil)
CompletionHandler(true)
}
let swipeConfiguration = UISwipeActionsConfiguration(actions: [deleteAction, shareAction])
return swipeConfiguration
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.cellLayoutMarginsFollowReadableWidth = true
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return restaurantNames.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "datacell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! RestaurantTableViewCell
// Configure the cell...
cell.nameLabel.text = restaurantNames[indexPath.row]
cell.locationLabel.text = restaurantLocations[indexPath.row]
cell.typeLabel.text = restaurantTypes[indexPath.row]
cell.thumbnailImageView.image = UIImage(named: restaurantImages[indexPath.row])
// Solution to exercise 2
cell.heartImageView.isHidden = !self.restaurantIsVisited[indexPath.row]
return cell
}
// MARK: - UITableViewDelegate Methods
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Create an option menu as an action sheet
let optionMenu = UIAlertController(title: nil, message: "What do you want to do?", preferredStyle: .actionSheet)
// For iPad
if let popoverController = optionMenu.popoverPresentationController {
if let cell = tableView.cellForRow(at: indexPath) {
popoverController.sourceView = cell
popoverController.sourceRect = cell.bounds
}
}
// Add actions to the menu
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
optionMenu.addAction(cancelAction)
// Add Call action
let callActionHandler = { (action:UIAlertAction!) -> Void in
let alertMessage = UIAlertController(title: "Service Unavailable", message: "Sorry, the call feature is not available yet. Please retry later.", preferredStyle: .alert)
alertMessage.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alertMessage, animated: true, completion: nil)
}
let callAction = UIAlertAction(title: "Call " + "123-000-\(indexPath.row)", style: .default, handler: callActionHandler)
optionMenu.addAction(callAction)
// Check-in action
let checkInTitle = self.restaurantIsVisited[indexPath.row] ? "Undo Check in" : "Check in"
let checkInAction = UIAlertAction(title: checkInTitle, style: .default, handler: {
(action:UIAlertAction!) -> Void in
let cell = tableView.cellForRow(at: indexPath) as? RestaurantTableViewCell
// Solution to exercise 1
// cell?.accessoryType = self.restaurantIsVisited[indexPath.row] ? .none : .checkmark
// Solution to exercise 2
// We use the isHidden property to control whether the image view is displayed or not
cell?.heartImageView.isHidden = self.restaurantIsVisited[indexPath.row]
self.restaurantIsVisited[indexPath.row] = self.restaurantIsVisited[indexPath.row] ? false : true
})
optionMenu.addAction(checkInAction)
// Display the menu
present(optionMenu, animated: true, completion: nil)
// Deselect a row
tableView.deselectRow(at: indexPath, animated: false)
}
}
向左滑即有新功能「Share」
按下 Share 按钮,就会跳出分享画面
覆蓋以下程式码,有图片的时候就可以分享图片
//
// RestaurantTableViewController.swift
// FoodPin
//
// Created by Simon Ng on 28/10/2019.
// Copyright © 2019 AppCoda. All rights reserved.
//
import UIKit
class RestaurantTableViewController: UITableViewController {
var restaurantNames = ["Cafe Deadend", "Homei", "Teakha", "Cafe Loisl", "Petite Oyster", "For Kee Restaurant", "Po's Atelier", "Bourke Street Bakery", "Haigh's Chocolate", "Palomino Espresso", "Upstate", "Traif", "Graham Avenue Meats", "Waffle & Wolf", "Five Leaves", "Cafe Lore", "Confessional", "Barrafina", "Donostia", "Royal Oak", "CASK Pub and Kitchen"]
var restaurantLocations = ["Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Sydney", "Sydney", "Sydney", "New York", "New York", "New York", "New York", "New York", "New York", "New York", "London", "London", "London", "London"]
var restaurantTypes = ["Coffee & Tea Shop", "Cafe", "Tea House", "Austrian / Causual Drink", "French", "Bakery", "Bakery", "Chocolate", "Cafe", "American / Seafood", "American", "American", "Breakfast & Brunch", "Coffee & Tea", "Coffee & Tea", "Latin American", "Spanish", "Spanish", "Spanish", "British", "Thai"]
var restaurantImages = ["cafedeadend", "homei", "teakha", "cafeloisl", "petiteoyster", "forkeerestaurant", "posatelier", "bourkestreetbakery", "haighschocolate", "palominoespresso", "upstate", "traif", "grahamavenuemeats", "wafflewolf", "fiveleaves", "cafelore", "confessional", "barrafina", "donostia", "royaloak", "caskpubkitchen"]
var restaurantIsVisited = Array(repeating: false, count: 21)
/*override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{
restaurantNames.remove(at: indexPath.row)
restaurantLocations.remove(at: indexPath.row)
restaurantTypes.remove(at: indexPath.row)
restaurantIsVisited.remove(at: indexPath.row)
restaurantImages.remove(at: indexPath.row)
}
//tableView.reloadData()
tableView.deleteRows(at: [indexPath], with: .fade)
}*/
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "Delete"){(action, sourceView, CompletionHandler) in
self.restaurantNames.remove(at: indexPath.row)
self.restaurantLocations.remove(at: indexPath.row)
self.restaurantTypes.remove(at: indexPath.row)
self.restaurantIsVisited.remove(at: indexPath.row)
self.restaurantImages.remove(at: indexPath.row)
CompletionHandler(true)
}
let shareAction = UIContextualAction(style: .normal, title: "Share") {(action, sourceView, CompletionHandler) in
let defaultText = "Just checking in at " + self.restaurantNames[indexPath.row]
let activityController : UIActivityViewController
if let imageToShare = UIImage(named: self.restaurantImages[indexPath.row]){
activityController = UIActivityViewController(activityItems: [defaultText, imageToShare], applicationActivities: nil)
}else{
activityController = UIActivityViewController(activityItems: [defaultText], applicationActivities: nil)
}
//(activityItems: [defaultText], applicationActivities: nil)
self.present(activityController, animated: true, completion: nil)
CompletionHandler(true)
}
let swipeConfiguration = UISwipeActionsConfiguration(actions: [deleteAction, shareAction])
return swipeConfiguration
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.cellLayoutMarginsFollowReadableWidth = true
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return restaurantNames.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "datacell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! RestaurantTableViewCell
// Configure the cell...
cell.nameLabel.text = restaurantNames[indexPath.row]
cell.locationLabel.text = restaurantLocations[indexPath.row]
cell.typeLabel.text = restaurantTypes[indexPath.row]
cell.thumbnailImageView.image = UIImage(named: restaurantImages[indexPath.row])
// Solution to exercise 2
cell.heartImageView.isHidden = !self.restaurantIsVisited[indexPath.row]
return cell
}
// MARK: - UITableViewDelegate Methods
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Create an option menu as an action sheet
let optionMenu = UIAlertController(title: nil, message: "What do you want to do?", preferredStyle: .actionSheet)
// For iPad
if let popoverController = optionMenu.popoverPresentationController {
if let cell = tableView.cellForRow(at: indexPath) {
popoverController.sourceView = cell
popoverController.sourceRect = cell.bounds
}
}
// Add actions to the menu
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
optionMenu.addAction(cancelAction)
// Add Call action
let callActionHandler = { (action:UIAlertAction!) -> Void in
let alertMessage = UIAlertController(title: "Service Unavailable", message: "Sorry, the call feature is not available yet. Please retry later.", preferredStyle: .alert)
alertMessage.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alertMessage, animated: true, completion: nil)
}
let callAction = UIAlertAction(title: "Call " + "123-000-\(indexPath.row)", style: .default, handler: callActionHandler)
optionMenu.addAction(callAction)
// Check-in action
let checkInTitle = self.restaurantIsVisited[indexPath.row] ? "Undo Check in" : "Check in"
let checkInAction = UIAlertAction(title: checkInTitle, style: .default, handler: {
(action:UIAlertAction!) -> Void in
let cell = tableView.cellForRow(at: indexPath) as? RestaurantTableViewCell
// Solution to exercise 1
// cell?.accessoryType = self.restaurantIsVisited[indexPath.row] ? .none : .checkmark
// Solution to exercise 2
// We use the isHidden property to control whether the image view is displayed or not
cell?.heartImageView.isHidden = self.restaurantIsVisited[indexPath.row]
self.restaurantIsVisited[indexPath.row] = self.restaurantIsVisited[indexPath.row] ? false : true
})
optionMenu.addAction(checkInAction)
// Display the menu
present(optionMenu, animated: true, completion: nil)
// Deselect a row
tableView.deselectRow(at: indexPath, animated: false)
}
}
覆蓋以下程式码,设定分享图片的颜色与 Icon
//
// RestaurantTableViewController.swift
// FoodPin
//
// Created by Simon Ng on 28/10/2019.
// Copyright © 2019 AppCoda. All rights reserved.
//
import UIKit
class RestaurantTableViewController: UITableViewController {
var restaurantNames = ["Cafe Deadend", "Homei", "Teakha", "Cafe Loisl", "Petite Oyster", "For Kee Restaurant", "Po's Atelier", "Bourke Street Bakery", "Haigh's Chocolate", "Palomino Espresso", "Upstate", "Traif", "Graham Avenue Meats", "Waffle & Wolf", "Five Leaves", "Cafe Lore", "Confessional", "Barrafina", "Donostia", "Royal Oak", "CASK Pub and Kitchen"]
var restaurantLocations = ["Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Sydney", "Sydney", "Sydney", "New York", "New York", "New York", "New York", "New York", "New York", "New York", "London", "London", "London", "London"]
var restaurantTypes = ["Coffee & Tea Shop", "Cafe", "Tea House", "Austrian / Causual Drink", "French", "Bakery", "Bakery", "Chocolate", "Cafe", "American / Seafood", "American", "American", "Breakfast & Brunch", "Coffee & Tea", "Coffee & Tea", "Latin American", "Spanish", "Spanish", "Spanish", "British", "Thai"]
var restaurantImages = ["cafedeadend", "homei", "teakha", "cafeloisl", "petiteoyster", "forkeerestaurant", "posatelier", "bourkestreetbakery", "haighschocolate", "palominoespresso", "upstate", "traif", "grahamavenuemeats", "wafflewolf", "fiveleaves", "cafelore", "confessional", "barrafina", "donostia", "royaloak", "caskpubkitchen"]
var restaurantIsVisited = Array(repeating: false, count: 21)
/*override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{
restaurantNames.remove(at: indexPath.row)
restaurantLocations.remove(at: indexPath.row)
restaurantTypes.remove(at: indexPath.row)
restaurantIsVisited.remove(at: indexPath.row)
restaurantImages.remove(at: indexPath.row)
}
//tableView.reloadData()
tableView.deleteRows(at: [indexPath], with: .fade)
}*/
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "Delete"){(action, sourceView, CompletionHandler) in
self.restaurantNames.remove(at: indexPath.row)
self.restaurantLocations.remove(at: indexPath.row)
self.restaurantTypes.remove(at: indexPath.row)
self.restaurantIsVisited.remove(at: indexPath.row)
self.restaurantImages.remove(at: indexPath.row)
CompletionHandler(true)
}
let shareAction = UIContextualAction(style: .normal, title: "Share") {(action, sourceView, CompletionHandler) in
let defaultText = "Just checking in at " + self.restaurantNames[indexPath.row]
let activityController : UIActivityViewController
if let imageToShare = UIImage(named: self.restaurantImages[indexPath.row]){
activityController = UIActivityViewController(activityItems: [defaultText, imageToShare], applicationActivities: nil)
}else{
activityController = UIActivityViewController(activityItems: [defaultText], applicationActivities: nil)
}
//(activityItems: [defaultText], applicationActivities: nil)
self.present(activityController, animated: true, completion: nil)
CompletionHandler(true)
}
deleteAction.backgroundColor = UIColor(red: 231.0/255.0, green: 76.0/255.0, blue: 60.0/255.0, alpha: 1.0)
deleteAction.image = UIImage(systemName: "trash")
shareAction.backgroundColor = UIColor(red: 254.0/255.0, green: 149.0/255.0, blue: 38.0/255.0, alpha: 1.0)
shareAction.image = UIImage(systemName: "square.and.arrow.up")
let swipeConfiguration = UISwipeActionsConfiguration(actions: [deleteAction, shareAction])
return swipeConfiguration
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.cellLayoutMarginsFollowReadableWidth = true
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return restaurantNames.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "datacell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! RestaurantTableViewCell
// Configure the cell...
cell.nameLabel.text = restaurantNames[indexPath.row]
cell.locationLabel.text = restaurantLocations[indexPath.row]
cell.typeLabel.text = restaurantTypes[indexPath.row]
cell.thumbnailImageView.image = UIImage(named: restaurantImages[indexPath.row])
// Solution to exercise 2
cell.heartImageView.isHidden = !self.restaurantIsVisited[indexPath.row]
return cell
}
// MARK: - UITableViewDelegate Methods
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Create an option menu as an action sheet
let optionMenu = UIAlertController(title: nil, message: "What do you want to do?", preferredStyle: .actionSheet)
// For iPad
if let popoverController = optionMenu.popoverPresentationController {
if let cell = tableView.cellForRow(at: indexPath) {
popoverController.sourceView = cell
popoverController.sourceRect = cell.bounds
}
}
// Add actions to the menu
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
optionMenu.addAction(cancelAction)
// Add Call action
let callActionHandler = { (action:UIAlertAction!) -> Void in
let alertMessage = UIAlertController(title: "Service Unavailable", message: "Sorry, the call feature is not available yet. Please retry later.", preferredStyle: .alert)
alertMessage.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alertMessage, animated: true, completion: nil)
}
let callAction = UIAlertAction(title: "Call " + "123-000-\(indexPath.row)", style: .default, handler: callActionHandler)
optionMenu.addAction(callAction)
// Check-in action
let checkInTitle = self.restaurantIsVisited[indexPath.row] ? "Undo Check in" : "Check in"
let checkInAction = UIAlertAction(title: checkInTitle, style: .default, handler: {
(action:UIAlertAction!) -> Void in
let cell = tableView.cellForRow(at: indexPath) as? RestaurantTableViewCell
// Solution to exercise 1
// cell?.accessoryType = self.restaurantIsVisited[indexPath.row] ? .none : .checkmark
// Solution to exercise 2
// We use the isHidden property to control whether the image view is displayed or not
cell?.heartImageView.isHidden = self.restaurantIsVisited[indexPath.row]
self.restaurantIsVisited[indexPath.row] = self.restaurantIsVisited[indexPath.row] ? false : true
})
optionMenu.addAction(checkInAction)
// Display the menu
present(optionMenu, animated: true, completion: nil)
// Deselect a row
tableView.deselectRow(at: indexPath, animated: false)
}
}
开启 Main.storyboard,按照图片选取到 Navigation Controller

按照图片选到,并且 Title 输入 FoodPin

回到 RestaurantTableViewController.swift ,覆蓋以下程式码
//
// RestaurantTableViewController.swift
// FoodPin
//
// Created by Simon Ng on 28/10/2019.
// Copyright © 2019 AppCoda. All rights reserved.
//
import UIKit
class RestaurantTableViewController: UITableViewController {
var restaurantNames = ["Cafe Deadend", "Homei", "Teakha", "Cafe Loisl", "Petite Oyster", "For Kee Restaurant", "Po's Atelier", "Bourke Street Bakery", "Haigh's Chocolate", "Palomino Espresso", "Upstate", "Traif", "Graham Avenue Meats", "Waffle & Wolf", "Five Leaves", "Cafe Lore", "Confessional", "Barrafina", "Donostia", "Royal Oak", "CASK Pub and Kitchen"]
var restaurantLocations = ["Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Sydney", "Sydney", "Sydney", "New York", "New York", "New York", "New York", "New York", "New York", "New York", "London", "London", "London", "London"]
var restaurantTypes = ["Coffee & Tea Shop", "Cafe", "Tea House", "Austrian / Causual Drink", "French", "Bakery", "Bakery", "Chocolate", "Cafe", "American / Seafood", "American", "American", "Breakfast & Brunch", "Coffee & Tea", "Coffee & Tea", "Latin American", "Spanish", "Spanish", "Spanish", "British", "Thai"]
var restaurantImages = ["cafedeadend", "homei", "teakha", "cafeloisl", "petiteoyster", "forkeerestaurant", "posatelier", "bourkestreetbakery", "haighschocolate", "palominoespresso", "upstate", "traif", "grahamavenuemeats", "wafflewolf", "fiveleaves", "cafelore", "confessional", "barrafina", "donostia", "royaloak", "caskpubkitchen"]
var restaurantIsVisited = Array(repeating: false, count: 21)
/*override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{
restaurantNames.remove(at: indexPath.row)
restaurantLocations.remove(at: indexPath.row)
restaurantTypes.remove(at: indexPath.row)
restaurantIsVisited.remove(at: indexPath.row)
restaurantImages.remove(at: indexPath.row)
}
//tableView.reloadData()
tableView.deleteRows(at: [indexPath], with: .fade)
}*/
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "Delete"){(action, sourceView, CompletionHandler) in
self.restaurantNames.remove(at: indexPath.row)
self.restaurantLocations.remove(at: indexPath.row)
self.restaurantTypes.remove(at: indexPath.row)
self.restaurantIsVisited.remove(at: indexPath.row)
self.restaurantImages.remove(at: indexPath.row)
CompletionHandler(true)
}
let shareAction = UIContextualAction(style: .normal, title: "Share") {(action, sourceView, CompletionHandler) in
let defaultText = "Just checking in at " + self.restaurantNames[indexPath.row]
let activityController : UIActivityViewController
if let imageToShare = UIImage(named: self.restaurantImages[indexPath.row]){
activityController = UIActivityViewController(activityItems: [defaultText, imageToShare], applicationActivities: nil)
}else{
activityController = UIActivityViewController(activityItems: [defaultText], applicationActivities: nil)
}
//(activityItems: [defaultText], applicationActivities: nil)
self.present(activityController, animated: true, completion: nil)
CompletionHandler(true)
}
deleteAction.backgroundColor = UIColor(red: 231.0/255.0, green: 76.0/255.0, blue: 60.0/255.0, alpha: 1.0)
deleteAction.image = UIImage(systemName: "trash")
shareAction.backgroundColor = UIColor(red: 254.0/255.0, green: 149.0/255.0, blue: 38.0/255.0, alpha: 1.0)
shareAction.image = UIImage(systemName: "square.and.arrow.up")
let swipeConfiguration = UISwipeActionsConfiguration(actions: [deleteAction, shareAction])
return swipeConfiguration
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.cellLayoutMarginsFollowReadableWidth = true
navigationController?.navigationBar.prefersLargeTitles = true
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return restaurantNames.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "datacell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! RestaurantTableViewCell
// Configure the cell...
cell.nameLabel.text = restaurantNames[indexPath.row]
cell.locationLabel.text = restaurantLocations[indexPath.row]
cell.typeLabel.text = restaurantTypes[indexPath.row]
cell.thumbnailImageView.image = UIImage(named: restaurantImages[indexPath.row])
// Solution to exercise 2
cell.heartImageView.isHidden = !self.restaurantIsVisited[indexPath.row]
return cell
}
// MARK: - UITableViewDelegate Methods
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Create an option menu as an action sheet
let optionMenu = UIAlertController(title: nil, message: "What do you want to do?", preferredStyle: .actionSheet)
// For iPad
if let popoverController = optionMenu.popoverPresentationController {
if let cell = tableView.cellForRow(at: indexPath) {
popoverController.sourceView = cell
popoverController.sourceRect = cell.bounds
}
}
// Add actions to the menu
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
optionMenu.addAction(cancelAction)
// Add Call action
let callActionHandler = { (action:UIAlertAction!) -> Void in
let alertMessage = UIAlertController(title: "Service Unavailable", message: "Sorry, the call feature is not available yet. Please retry later.", preferredStyle: .alert)
alertMessage.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alertMessage, animated: true, completion: nil)
}
let callAction = UIAlertAction(title: "Call " + "123-000-\(indexPath.row)", style: .default, handler: callActionHandler)
optionMenu.addAction(callAction)
// Check-in action
let checkInTitle = self.restaurantIsVisited[indexPath.row] ? "Undo Check in" : "Check in"
let checkInAction = UIAlertAction(title: checkInTitle, style: .default, handler: {
(action:UIAlertAction!) -> Void in
let cell = tableView.cellForRow(at: indexPath) as? RestaurantTableViewCell
// Solution to exercise 1
// cell?.accessoryType = self.restaurantIsVisited[indexPath.row] ? .none : .checkmark
// Solution to exercise 2
// We use the isHidden property to control whether the image view is displayed or not
cell?.heartImageView.isHidden = self.restaurantIsVisited[indexPath.row]
self.restaurantIsVisited[indexPath.row] = self.restaurantIsVisited[indexPath.row] ? false : true
})
optionMenu.addAction(checkInAction)
// Display the menu
present(optionMenu, animated: true, completion: nil)
// Deselect a row
tableView.deselectRow(at: indexPath, animated: false)
}
}
回到 Main.storyboard ,拉一个新的 View Controller 到画面最右边

再拉一个 Image View 到最右边的 View Contorller

设定约束条件
设定 Content Mode 为 Aspect Fill
按照图片选到 datacell
按著 datacell 以及 ctrl 按键,连结第二个 View Controller,选择 Show
现在按下餐厅,他会跳到第二个画面,还是会跳出讯息框
覆蓋以下程式码,关闭跳出的视窗
//
// RestaurantTableViewController.swift
// FoodPin
//
// Created by Simon Ng on 28/10/2019.
// Copyright © 2019 AppCoda. All rights reserved.
//
import UIKit
class RestaurantTableViewController: UITableViewController {
var restaurantNames = ["Cafe Deadend", "Homei", "Teakha", "Cafe Loisl", "Petite Oyster", "For Kee Restaurant", "Po's Atelier", "Bourke Street Bakery", "Haigh's Chocolate", "Palomino Espresso", "Upstate", "Traif", "Graham Avenue Meats", "Waffle & Wolf", "Five Leaves", "Cafe Lore", "Confessional", "Barrafina", "Donostia", "Royal Oak", "CASK Pub and Kitchen"]
var restaurantLocations = ["Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Sydney", "Sydney", "Sydney", "New York", "New York", "New York", "New York", "New York", "New York", "New York", "London", "London", "London", "London"]
var restaurantTypes = ["Coffee & Tea Shop", "Cafe", "Tea House", "Austrian / Causual Drink", "French", "Bakery", "Bakery", "Chocolate", "Cafe", "American / Seafood", "American", "American", "Breakfast & Brunch", "Coffee & Tea", "Coffee & Tea", "Latin American", "Spanish", "Spanish", "Spanish", "British", "Thai"]
var restaurantImages = ["cafedeadend", "homei", "teakha", "cafeloisl", "petiteoyster", "forkeerestaurant", "posatelier", "bourkestreetbakery", "haighschocolate", "palominoespresso", "upstate", "traif", "grahamavenuemeats", "wafflewolf", "fiveleaves", "cafelore", "confessional", "barrafina", "donostia", "royaloak", "caskpubkitchen"]
var restaurantIsVisited = Array(repeating: false, count: 21)
/*override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{
restaurantNames.remove(at: indexPath.row)
restaurantLocations.remove(at: indexPath.row)
restaurantTypes.remove(at: indexPath.row)
restaurantIsVisited.remove(at: indexPath.row)
restaurantImages.remove(at: indexPath.row)
}
//tableView.reloadData()
tableView.deleteRows(at: [indexPath], with: .fade)
}*/
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "Delete"){(action, sourceView, CompletionHandler) in
self.restaurantNames.remove(at: indexPath.row)
self.restaurantLocations.remove(at: indexPath.row)
self.restaurantTypes.remove(at: indexPath.row)
self.restaurantIsVisited.remove(at: indexPath.row)
self.restaurantImages.remove(at: indexPath.row)
CompletionHandler(true)
}
let shareAction = UIContextualAction(style: .normal, title: "Share") {(action, sourceView, CompletionHandler) in
let defaultText = "Just checking in at " + self.restaurantNames[indexPath.row]
let activityController : UIActivityViewController
if let imageToShare = UIImage(named: self.restaurantImages[indexPath.row]){
activityController = UIActivityViewController(activityItems: [defaultText, imageToShare], applicationActivities: nil)
}else{
activityController = UIActivityViewController(activityItems: [defaultText], applicationActivities: nil)
}
//(activityItems: [defaultText], applicationActivities: nil)
self.present(activityController, animated: true, completion: nil)
CompletionHandler(true)
}
deleteAction.backgroundColor = UIColor(red: 231.0/255.0, green: 76.0/255.0, blue: 60.0/255.0, alpha: 1.0)
deleteAction.image = UIImage(systemName: "trash")
shareAction.backgroundColor = UIColor(red: 254.0/255.0, green: 149.0/255.0, blue: 38.0/255.0, alpha: 1.0)
shareAction.image = UIImage(systemName: "square.and.arrow.up")
let swipeConfiguration = UISwipeActionsConfiguration(actions: [deleteAction, shareAction])
return swipeConfiguration
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.cellLayoutMarginsFollowReadableWidth = true
navigationController?.navigationBar.prefersLargeTitles = true
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return restaurantNames.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "datacell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! RestaurantTableViewCell
// Configure the cell...
cell.nameLabel.text = restaurantNames[indexPath.row]
cell.locationLabel.text = restaurantLocations[indexPath.row]
cell.typeLabel.text = restaurantTypes[indexPath.row]
cell.thumbnailImageView.image = UIImage(named: restaurantImages[indexPath.row])
// Solution to exercise 2
cell.heartImageView.isHidden = !self.restaurantIsVisited[indexPath.row]
return cell
}
// MARK: - UITableViewDelegate Methods
/*override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Create an option menu as an action sheet
let optionMenu = UIAlertController(title: nil, message: "What do you want to do?", preferredStyle: .actionSheet)
// For iPad
if let popoverController = optionMenu.popoverPresentationController {
if let cell = tableView.cellForRow(at: indexPath) {
popoverController.sourceView = cell
popoverController.sourceRect = cell.bounds
}
}
// Add actions to the menu
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
optionMenu.addAction(cancelAction)
// Add Call action
let callActionHandler = { (action:UIAlertAction!) -> Void in
let alertMessage = UIAlertController(title: "Service Unavailable", message: "Sorry, the call feature is not available yet. Please retry later.", preferredStyle: .alert)
alertMessage.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alertMessage, animated: true, completion: nil)
}
let callAction = UIAlertAction(title: "Call " + "123-000-\(indexPath.row)", style: .default, handler: callActionHandler)
optionMenu.addAction(callAction)
// Check-in action
let checkInTitle = self.restaurantIsVisited[indexPath.row] ? "Undo Check in" : "Check in"
let checkInAction = UIAlertAction(title: checkInTitle, style: .default, handler: {
(action:UIAlertAction!) -> Void in
let cell = tableView.cellForRow(at: indexPath) as? RestaurantTableViewCell
// Solution to exercise 1
// cell?.accessoryType = self.restaurantIsVisited[indexPath.row] ? .none : .checkmark
// Solution to exercise 2
// We use the isHidden property to control whether the image view is displayed or not
cell?.heartImageView.isHidden = self.restaurantIsVisited[indexPath.row]
self.restaurantIsVisited[indexPath.row] = self.restaurantIsVisited[indexPath.row] ? false : true
})
optionMenu.addAction(checkInAction)
// Display the menu
present(optionMenu, animated: true, completion: nil)
// Deselect a row
tableView.deselectRow(at: indexPath, animated: false)
}*/
}
再次点进餐厅,已经不会跳出那个视窗了
新增一个档案,名称 RestaurantDetailViewController
根据图片关联程式码

开启 RestaurantDetailViewController.swift,覆蓋以下程式码
//
// RestaurantDetailViewController.swift
// FoodPin
//
// Created by SHXJ on 2020/11/19.
// Copyright © 2020 AppCoda. All rights reserved.
//
import UIKit
class RestaurantDetailViewController: UIViewController {
@IBOutlet var restaurantImageView: UIImageView!
var restaurantImageName = ""
override func viewDidLoad() {
super.viewDidLoad()
restaurantImageView.image = UIImage(named: restaurantImageName)
// Do any additional setup after loading the view.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
将第13行的程式连到最右边的 View Controller

点击中间那个蓝蓝的,在 ID输入 showRestaurantDetail
开启 RestaurantTableViewController.swift 覆蓋以下程式码
//
// RestaurantTableViewController.swift
// FoodPin
//
// Created by Simon Ng on 28/10/2019.
// Copyright © 2019 AppCoda. All rights reserved.
//
import UIKit
class RestaurantTableViewController: UITableViewController {
var restaurantNames = ["Cafe Deadend", "Homei", "Teakha", "Cafe Loisl", "Petite Oyster", "For Kee Restaurant", "Po's Atelier", "Bourke Street Bakery", "Haigh's Chocolate", "Palomino Espresso", "Upstate", "Traif", "Graham Avenue Meats", "Waffle & Wolf", "Five Leaves", "Cafe Lore", "Confessional", "Barrafina", "Donostia", "Royal Oak", "CASK Pub and Kitchen"]
var restaurantLocations = ["Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Sydney", "Sydney", "Sydney", "New York", "New York", "New York", "New York", "New York", "New York", "New York", "London", "London", "London", "London"]
var restaurantTypes = ["Coffee & Tea Shop", "Cafe", "Tea House", "Austrian / Causual Drink", "French", "Bakery", "Bakery", "Chocolate", "Cafe", "American / Seafood", "American", "American", "Breakfast & Brunch", "Coffee & Tea", "Coffee & Tea", "Latin American", "Spanish", "Spanish", "Spanish", "British", "Thai"]
var restaurantImages = ["cafedeadend", "homei", "teakha", "cafeloisl", "petiteoyster", "forkeerestaurant", "posatelier", "bourkestreetbakery", "haighschocolate", "palominoespresso", "upstate", "traif", "grahamavenuemeats", "wafflewolf", "fiveleaves", "cafelore", "confessional", "barrafina", "donostia", "royaloak", "caskpubkitchen"]
var restaurantIsVisited = Array(repeating: false, count: 21)
/*override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{
restaurantNames.remove(at: indexPath.row)
restaurantLocations.remove(at: indexPath.row)
restaurantTypes.remove(at: indexPath.row)
restaurantIsVisited.remove(at: indexPath.row)
restaurantImages.remove(at: indexPath.row)
}
//tableView.reloadData()
tableView.deleteRows(at: [indexPath], with: .fade)
}*/
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showRestaurantDetail"{
if let indexPath = tableView.indexPathForSelectedRow{
let destinationController = segue.destination as! RestaurantDetailViewController
destinationController.restaurantImageName = restaurantImages[indexPath.row]
}
}
}
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "Delete"){(action, sourceView, CompletionHandler) in
self.restaurantNames.remove(at: indexPath.row)
self.restaurantLocations.remove(at: indexPath.row)
self.restaurantTypes.remove(at: indexPath.row)
self.restaurantIsVisited.remove(at: indexPath.row)
self.restaurantImages.remove(at: indexPath.row)
CompletionHandler(true)
}
let shareAction = UIContextualAction(style: .normal, title: "Share") {(action, sourceView, CompletionHandler) in
let defaultText = "Just checking in at " + self.restaurantNames[indexPath.row]
let activityController : UIActivityViewController
if let imageToShare = UIImage(named: self.restaurantImages[indexPath.row]){
activityController = UIActivityViewController(activityItems: [defaultText, imageToShare], applicationActivities: nil)
}else{
activityController = UIActivityViewController(activityItems: [defaultText], applicationActivities: nil)
}
//(activityItems: [defaultText], applicationActivities: nil)
self.present(activityController, animated: true, completion: nil)
CompletionHandler(true)
}
deleteAction.backgroundColor = UIColor(red: 231.0/255.0, green: 76.0/255.0, blue: 60.0/255.0, alpha: 1.0)
deleteAction.image = UIImage(systemName: "trash")
shareAction.backgroundColor = UIColor(red: 254.0/255.0, green: 149.0/255.0, blue: 38.0/255.0, alpha: 1.0)
shareAction.image = UIImage(systemName: "square.and.arrow.up")
let swipeConfiguration = UISwipeActionsConfiguration(actions: [deleteAction, shareAction])
return swipeConfiguration
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.cellLayoutMarginsFollowReadableWidth = true
navigationController?.navigationBar.prefersLargeTitles = true
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return restaurantNames.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "datacell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! RestaurantTableViewCell
// Configure the cell...
cell.nameLabel.text = restaurantNames[indexPath.row]
cell.locationLabel.text = restaurantLocations[indexPath.row]
cell.typeLabel.text = restaurantTypes[indexPath.row]
cell.thumbnailImageView.image = UIImage(named: restaurantImages[indexPath.row])
// Solution to exercise 2
cell.heartImageView.isHidden = !self.restaurantIsVisited[indexPath.row]
return cell
}
// MARK: - UITableViewDelegate Methods
/*override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Create an option menu as an action sheet
let optionMenu = UIAlertController(title: nil, message: "What do you want to do?", preferredStyle: .actionSheet)
// For iPad
if let popoverController = optionMenu.popoverPresentationController {
if let cell = tableView.cellForRow(at: indexPath) {
popoverController.sourceView = cell
popoverController.sourceRect = cell.bounds
}
}
// Add actions to the menu
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
optionMenu.addAction(cancelAction)
// Add Call action
let callActionHandler = { (action:UIAlertAction!) -> Void in
let alertMessage = UIAlertController(title: "Service Unavailable", message: "Sorry, the call feature is not available yet. Please retry later.", preferredStyle: .alert)
alertMessage.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alertMessage, animated: true, completion: nil)
}
let callAction = UIAlertAction(title: "Call " + "123-000-\(indexPath.row)", style: .default, handler: callActionHandler)
optionMenu.addAction(callAction)
// Check-in action
let checkInTitle = self.restaurantIsVisited[indexPath.row] ? "Undo Check in" : "Check in"
let checkInAction = UIAlertAction(title: checkInTitle, style: .default, handler: {
(action:UIAlertAction!) -> Void in
let cell = tableView.cellForRow(at: indexPath) as? RestaurantTableViewCell
// Solution to exercise 1
// cell?.accessoryType = self.restaurantIsVisited[indexPath.row] ? .none : .checkmark
// Solution to exercise 2
// We use the isHidden property to control whether the image view is displayed or not
cell?.heartImageView.isHidden = self.restaurantIsVisited[indexPath.row]
self.restaurantIsVisited[indexPath.row] = self.restaurantIsVisited[indexPath.row] ? false : true
})
optionMenu.addAction(checkInAction)
// Display the menu
present(optionMenu, animated: true, completion: nil)
// Deselect a row
tableView.deselectRow(at: indexPath, animated: false)
}*/
}
再来点进一个餐厅里面,就会出现大图了
- 受保护的内容: NAS 版 Mathbot 管理网站与 Linebot 启动方法 - 2024 年 11 月 15 日
- Realtime 啥鬼的 - 2021 年 6 月 15 日
- nodejs 数学游戏 - 2021 年 6 月 8 日


















