2017-03-02 53 views
0

我在迅速合并两个阵列以形成表与切片在夫特

struct Objects { 
    var sectionName: String! 
    var sectionObjects: [Brands]! 
} 

struct Brands { 
    var id: String! 
    var name: String! 
} 

这两个结构而那些三个空阵列

lazy var sortedLetters = [String]() 
lazy var sortedBrands = [Brands]() 
lazy var objectBrands = [Objects]() 

的sortedLetters阵列是用于实现代码如下制备标题。

sortedBrands是一个品牌数组,其中我试图填充品牌的名称,如果该字母与sortedLetters数组相同。

objectBrands是一个对象数组,在你将在tableview函数中看到的对象数组中,我得到了Brands和header的数据。

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
      return requests.objectBrands[section].sectionName 
    } 

    func numberOfSections(in tableView: UITableView) -> Int { 
      return objectBrands.count 
    } 

    func sectionIndexTitles(for tableView: UITableView) -> [String]? { 
      return sortedLetters 

    } 

    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { 
       let header = view as! UITableViewHeaderFooterView 
       header.textLabel?.font = UIFont(name: "Helvetica", size: 11) 
       header.textLabel?.textColor = .black 
       header.contentView.backgroundColor = .darkGray 
    } 

    func tableView(_ tableView: UITableView, 
        heightForRowAt indexPath: IndexPath) -> CGFloat { 
      return 70 

    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
      return requests.objectBrands[section].sectionObjects.count 
    } 

而在这for loop我试图追加数组sortedLetters的eacher信里面的品牌。

for startNames in self.sortedLetters { 
    self.objectBrands.append(Objects(sectionName: startNames, sectionObjects: self.sortedBrands)) 
} 

虽然它追加所有品牌。 而在我的桌子里我有这样的东西

Header : "A" Cells : All brands Header : "B" Cells : All brands 这是正常的原因我错过了我的循环中的东西。

我该如何在swift语法中更改我的for loop,因此它可以在字母“A”中添加以“A”开头的每个品牌名称?

所以它会显示这样的事情

Header : "A" Cells : Brands that start with A Header : "B" Cells : Brands that start with B 等等

非常感谢!

回答

2

// Iterate each Header Name for startNames in self.sortedLetters { // Filter out the brands which name starts with startNames // Here, I also sorted the brand by its name. let brands = self.sortedBrands.filter() {$0.name.hasPrefix(startNames)}.sorted(by: {$0.name < $1.name}) self.objectBrands.append(Objects(sectionName: startNames, sectionObjects:brands)) }

+1

这就是我正在寻找的! –

1

使用字典为您的部分,其中的关键是段落标题的字母和值是包含您的模式,这种部分的数组。您将需要单独对键进行排序(或者只是预先计算并将排序键存储在数组中;如果您想要稀疏字典,则每次数据更改时都需要执行此操作(即,只显示包含项的段的标题或仅显示一次如果你想不管他们是否是空的)显示所有的头

var sections: [String: [ModelType]] = [:] 

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     return sortedSectionKeys[section] // sortedSectionKeys = sections.keys.sorted {$0.0 < $0.1} 
} 

func numberOfSections(in tableView: UITableView) -> Int { 
     return sections.keys.count 
} 

func sectionIndexTitles(for tableView: UITableView) -> [String]? { 
     return sortedSectionKeys 

} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return sections[sortedSectionKeys[section]]?.count ?? 0 
} 
1

您可以按首字母这样的筛选sortedLetters阵列:

sortedLetters.filter({ $0.substring(to: $0.index($0.startIndex, offsetBy: 1)).lowercased() == "a" }) 

这将只显示品牌是以“a”(小写或大写)为例,你可以用你使用的任何变量替换“a” termine你所在的部分。