我想将我的项目(城市名称)分为SplitViewController
部分中的2个部分,名称分别为:国内和国际。如何将项目分类到一个tableView MasterViewController - Swift 3
我使用的dataModel
一个enum
与在MasterViewController
这一个init
是我到目前为止所。
的DataModel ...
import UIKit
enum welcomeImage {
case austin, athens, barcelona, losAngeles, newYork, palmSprings, paris, portland, reykjavik, rome
}
class Destination {
let name: String
var description: String
var destinationType: String
let imageName: welcomeImage
init(name: String, description: String, destinationType: String, imageName: welcomeImage) {
self.name = name
self.description = description
self.destinationType = destinationType
self.imageName = imageName
}
func welcomeImage() -> UIImage? {
switch self.imageName {
case .athens:
return UIImage(named: "Athens.jpg")
case .austin:
return UIImage(named: "Austin.jpg")
case .barcelona:
return UIImage(named: "Barcelona.jpg")
case .losAngeles:
return UIImage(named: "Los Angeles.jpg")
case .newYork:
return UIImage(named: "New York.jpg")
case .palmSprings:
return UIImage(named: "Palm Springs.jpg")
}
}
MasterViewController ...
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
self.cities.append(Destination(name: "Austin, Texas", description: "The lonestar state, BBQ and live music! Just watch out for the fire ants and always bring a bottle of water where ever you go.", destinationType: "", imageName: .austin))
self.cities.append(Destination(name: "Athens, Greece", description: "The birthplace of democracy. The freshest food in Europe! Being in Athens, you get a genuine feeling of antiquity.", destinationType: "", imageName: .athens))
self.cities.append(Destination(name: "Barcelona, Spain", description: "Home of Gaudi and the epic construciton of La Sagrada Familia. The croissants in Barcelona are actually better than those in Paris.", destinationType: "", imageName: .barcelona))
self.cities.append(Destination(name: "Los Angeles, California", description: "San Gabriel mountains, swimming pools, palm trees, beaches and high-energy...", destinationType: "", imageName: .losAngeles))
self.cities.append(Destination(name: "New York, New York", description: "The big apple", destinationType: "", imageName: .newYork))
self.cities.append(Destination(name: "Palm Springs, California", description: "Warm Desert Breezes", destinationType: "", imageName: .palmSprings))
}
委托方法...
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.cities.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let city = self.cities[indexPath.row]
cell.textLabel?.text = city.name
if let imageView = cell.imageView, let itemImage = city.welcomeImage() {
imageView.image = itemImage
} else {
cell.imageView?.image = nil
}
return cell
}
阵列中MasterViewController ...
let sectionTitles: [String] = ["Domestic", "International"]
var domesticCity: [welcomeImage] = [.austin, .losAngeles, .newYork, .palmSprings]
var internationalCity: [welcomeImage] = [.athens, .barcelona]
我不确定要在destinationType
中放置什么,以便将城市划分为tableView
中的国内和国际的相应部分。
任何帮助表示赞赏。谢谢!
感谢编辑帮助@RyanPoolos –
如果你显示你的'numberOfSections'完整的方法。并且'numberOfRowsInSection'和'cellForItem ...'我们可能会有更好的帮助。 –