可以刪除 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) {
}
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)
}*/
}

再來點進一個餐廳裡面,就會出現大圖了

SHXJ
Latest posts by SHXJ (see all)

發佈留言