2017-04-25 30 views
-1

我想将我的项目(城市名称)分为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中的国内和国际的相应部分。

任何帮助表示赞赏。谢谢!

+0

感谢编辑帮助@RyanPoolos –

+0

如果你显示你的'numberOfSections'完整的方法。并且'numberOfRowsInSection'和'cellForItem ...'我们可能会有更好的帮助。 –

回答

0

最简单的方法是为每个表格部分填充一个数组。当您创建每个目的地并添加到自我时,请将destinationType设置为“国内”或“国际”。一旦完成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'的数组字典来管理多个部分,但只有两个部分可以显式定义数组。

+0

谢谢@FryAnEgg。结果并不如预期。我在每个部分结束了2个部分的混合国内和国际引用。 –

相关问题