我在迅速合并两个阵列以形成表与切片在夫特
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
等等
非常感谢!
这就是我正在寻找的! –