然后用下面的『restaurants』阵列取代:
var restaurants:[Restaurant] = [ Restaurant(name: "Cafe Deadend", type: "Coffee & Tea Shop", location: "Hong Kong", 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: "Hong Kong", 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: "New York", 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: "Thai Cafe", type: "Thai", location: "London", image: "thaicafe.jpg", isVisited: false) ]
完成之后,Xcode 会出现几个错误警告。很明显的啦,因为我们有部分的程式内容还是参考先前那几个阵列,所以我们要将其变更几个新的 resturants阵列。
首先我们要改变 tableView(_:numberOfRowsInSection)方法:
return self.restaurantNames.count
修改成:
return self.restaurants.count
接着更新 tableView(_:cellForRowAtIndPath:) 方法:
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])
这几行出错的部分将他修改成:
let restaurant = restaurants [indexPath.row]
cell.nameLabel!.text = restaurant.name cell.locationLabel!.text = restaurant.location cell.typeLabel!.text = restaurant.type cell.thumbnailImageView!.image = UIImage(named: restaurant.image)
因为现在所有的餐厅资讯现在都从 restaurant 物件取得。你可以使用点与罚来取得属性质。
再来将tableView(_:commitEditintStyle) 与 tableVIew
(_:editActionsForRowAtIndexPath:) 方法内的这几行程式
用这行程式取代:
self.restaurants.removeAtIndex(indexPath.row)
最后,将prepareForSegue 方法变更如下:
destinationController.restaurantImage = self.restaurantImage[indexPath.row]
修改成:
destinationController.restaurantImage = self.restaurants[indexPath.row].image
现在小伙伴们又可以执行你的APP。APP的外观感觉跟之前没有什么不同,不过我们已经重构程式,使用了Restaurant 类别。 借由类别将资料阵列转换为 Restaurant 阵列,程式的可读性也变高了!
今天小伙伴也许会有『坑爹啊!一整篇下来整个APP连变都没变!』,恩! 没错!就是连变都没变,因为这个观念很重要,所以在这边特别提出来说明,将来小伙伴们的项目越来越复杂,今天是还好我们之前只有五个阵列要管理,倘若将来你的项目里面有一百个阵列呢?
最近有越来越多的小伙伴跟阿达说有在跟着做了,真的好开心,感谢各位小伙伴的支持与爱护,让我们一起用一行一行的代码,一步一步的实现我们的梦想,我是阿达,我们下回见。
|