關于面向對象編程(Object Oriented Programming ,OOP)的知識就不多介紹了,這不是一門編程語言中的概念,而是編程方法。OOP最好的一地方是,它讓復雜的軟件分解成一個個小的部分,方便程序員開放和管理。
這一篇文章就把上一篇文章開始用Swift開發iOS 10 - 10 Navigation Controller的介紹和Segue的代碼整理成OOP形式。
重新修改FoodPin項目
之前在
RestaurantTableViewController
類中,定義了restaurantNames
,restaurantImages
,restaurantLocations
,restaurantTypes
四個數組變量,分別表示restaurant的名字,圖片名稱,位置,類型。每個restaurant的信息分散在四個數組中,而且數組的內容順序要對應,現在把四個信息組成在一個類中Restaurant
。添加類文件
Restaurant.swift
import Foundation
class Restaurant {
var name = ""
var type = ""
var location = ""
var image = ""
var isVisited = false
// 1
init(name: String, type: String, location: String, image: String,
isVisited: Bool) {
self.name = name
self.type = type
self.location = location
self.image = image
self.isVisited = isVisited
}
}
- 1 自定義的初始化方法。在Swift中,類的所有變量都需要被初始化,或者聲明為可選值。
-
用
Restaurant
對象數組代替RestaurantTableViewController
中的幾個數組變量。var restaurants:[Restaurant] = [ Restaurant(name: "Cafe Deadend", type: "Coffee & Tea Shop", location: "HongKong", image: "cafedeadend.jpg", isVisited: false), Restaurant(name: "Homei", type: "Cafe", location: "Hong Kong", image:"homei.jpg", isVisited: false), Restaurant(name: "Teakha", type: "Tea House", location: "Hong Kong", image:"teakha.jpg", isVisited: false), Restaurant(name: "Cafe loisl", type: "Austrian / Causual Drink", location: "Hong Kong", image: "cafeloisl.jpg", isVisited: false), Restaurant(name: "Petite Oyster", type: "French", location: "Hong Kong", image: "petiteoyster.jpg", isVisited: false), Restaurant(name: "For Kee Restaurant", type: "Bakery", location: "HongKong", image: "forkeerestaurant.jpg", isVisited: false), Restaurant(name: "Po's Atelier", type: "Bakery", location: "Hong Kong", image: "posatelier.jpg", isVisited: false), Restaurant(name: "Bourke Street Backery", type: "Chocolate", location: "Sydney", image: "bourkestreetbakery.jpg", isVisited: false), Restaurant(name: "Haigh's Chocolate", type: "Cafe", location: "Sydney", image: "haighschocolate.jpg", isVisited: false), Restaurant(name: "Palomino Espresso", type: "American / Seafood", location: "Sydney", image: "palominoespresso.jpg", isVisited: false), Restaurant(name: "Upstate", type: "American", location: "New York", image: "upstate.jpg", isVisited: false), Restaurant(name: "Traif", type: "American", location: "New York", image: "traif.jpg", isVisited: false), Restaurant(name: "Graham Avenue Meats", type: "Breakfast & Brunch", location: "New York", image: "grahamavenuemeats.jpg", isVisited: false), Restaurant(name: "Waffle & Wolf", type: "Coffee & Tea", location: "NewYork", image: "wafflewolf.jpg", isVisited: false), Restaurant(name: "Five Leaves", type: "Coffee & Tea", location: "New York",image: "fiveleaves.jpg", isVisited: false), Restaurant(name: "Cafe Lore", type: "Latin American", location: "New York", image: "cafelore.jpg", isVisited: false), Restaurant(name: "Confessional", type: "Spanish", location: "New York", image: "confessional.jpg", isVisited: false), Restaurant(name: "Barrafina", type: "Spanish", location: "London", image: "barrafina.jpg", isVisited: false), Restaurant(name: "Donostia", type: "Spanish", location: "London", image: "donostia.jpg", isVisited: false), Restaurant(name: "Royal Oak", type: "British", location: "London", image: "royaloak.jpg", isVisited: false), Restaurant(name: "CASK Pub and Kitchen", type: "Thai", location: "London", image: "caskpubkitchen.jpg", isVisited: false) ]
更新
tableView(_:numberOfRowsInSection:)
:
override func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
return restaurants.count
}
- 更新
tableView(_:cellForRowAtIndexPath:)
:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier,
for: indexPath) as! RestaurantTableViewCell
// Configure the cell...
cell.nameLabel.text = restaurants[indexPath.row].name
cell.thumbnailImageView.image = UIImage(named: restaurants[indexPath.row].image)
cell.thumbnailImageView.layer.cornerRadius = 30.0
cell.thumbnailImageView.clipsToBounds = true
cell.locationLabel.text = restaurants[indexPath.row].location
cell.typeLabel.text = restaurants[indexPath.row].type
cell.accessoryType = restaurants[indexPath.row].isVisited ? .checkmark : .none
return cell
}
- 更新
tableView(_:commit:forRowAt:)
:
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
restaurants.remove(at: indexPath.row)
}
tableView.deleteRows(at: [indexPath], with: .fade)
}
-
更新
tableView(_:editActionsForRowAt:)
:override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { let shareAction = UITableViewRowAction(style: .default, title: "Share", handler: { (action, indexPath) -> Void in let defaultText = "Just checking in at " + self.restaurants[indexPath.row].name if let imageToShare = UIImage(named: self.restaurants[indexPath.row].image) { let activityController = UIActivityViewController(activityItems: [defaultText, imageToShare], applicationActivities: nil) self.present(activityController, animated: true, completion: nil) } }) let deleteAction = UITableViewRowAction(style: .default, title: "Delete", handler: { (action, indexPath) -> Void in self.restaurants.remove(at: indexPath.row) tableView.deleteRows(at: [indexPath], with: .fade) }) shareAction.backgroundColor = UIColor(red: 48.0/255.0, green: 173.0/255.0, blue: 99.0/255.0, alpha: 1.0) deleteAction.backgroundColor = UIColor(red: 202.0/255.0, green: 202.0/255.0, blue: 203.0/255.0, alpha: 1.0) return [deleteAction,shareAction] }
RestaurantDetailViewController
中的四個變量:
var nameText = ""
var locationText = ""
var typeText = ""
var restaurantImage = ""
更新為:var restaurant: Restaurant!
與之對應的viewDidLoad()
也修改為:
override func viewDidLoad() {
super.viewDidLoad()
restaurantImageView.image = UIImage(named: restaurant.image)
nameLabel.text = restaurant.name
locationLabel.text = restaurant.location
typeLabel.text = restaurant.type
}
- 最后
prepare(for:sender:)
方法也做對應修改:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showRestaurantDetail" {
if let indexPath = tableView.indexPathForSelectedRow {
let destinationController = segue.destination as! RestaurantDetailViewController
destinationController.restaurant = restaurants[indexPath.row]
}
}
}
運行沒有問題。
代碼
Beginning-iOS-Programming-with-Swift
說明
此文是學習appcode網站出的一本書 《Beginning iOS 10 Programming with Swift》 的一篇記錄
系列文章目錄
- 開始用Swift開發iOS 10 - 1 前言
- 開始用Swift開發iOS 10 - 2 Hello World!第一個Swift APP
- 開始用Swift開發iOS 10 - 3 介紹Auto Layout
- 開始用Swift開發iOS 10 - 4 用Stack View設計UI
- [開始用Swift開發iOS 10 - 5 原型的介紹]
- 開始用Swift開發iOS 10 - 6 創建簡單的Table Based App
- 開始用Swift開發iOS 10 - 7 定制Table Views
- 開始用Swift開發iOS 10 - 8 Table View和UIAlertController的交互
- 開始用Swift開發iOS 10 - 9 Table Row的刪除, UITableViewRowAction和UIActivityViewController的使用