我想将我的项目(城市名称)分类为SplitViewController
部分中的2个部分:国内和国际。
我在enum
中使用dataModel
并在init
中使用MasterViewController
这是我到目前为止所拥有的。
...的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
中将城市划分为国内和国际的相应部分。
感谢任何帮助。谢谢!
答案 0 :(得分:0)
最简单的方法是为每个表部分填充数组。创建每个目标并将其添加到self.cities时,将destinationType设置为“Domestic”或“International”。完成self.cities后,创建self.domesticCities和self.internationalCities数组,并通过self.cities并将每个城市添加到正确的destinationType数组。
然后:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(section == 0){
return self.domesticCities.count
} else if(section == 1){
return self.internationalCities.count
}
return 0; // should never get here
}
同样适用于cellForRowAt。
我做同样的事情,根据名字的第一个字母组织联系人,并支持右侧拇指索引(或他们称之为的任何东西)。您可以使用带有destinationType“keys”的数组字典来管理多个部分,但只有两个部分可以显式定义数组。