Ios 将两个数组组合在一起,形成Swift中包含节的表

Ios 将两个数组组合在一起,形成Swift中包含节的表,ios,swift,swift3,Ios,Swift,Swift3,我在swift里有这两个结构 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 ob

我在swift里有这两个结构

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数组用于在tableview中生成标题

sortedBrands是一个品牌数组,如果字母与sortedLetters数组相同,我将尝试填充品牌名称

objectBrands是一个对象数组,正如您将在tableview函数中看到的那样,我从中获取品牌数据和标题字母

 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循环中
我试图在数组的每个字母
sortedletter
中添加品牌

for startNames in self.sortedLetters {
  self.objectBrands.append(Objects(sectionName: startNames, sectionObjects: self.sortedBrands))
}
尽管它附加了所有品牌。 在我的桌面视图中,我有这样的东西

标题:“A”
电池:所有品牌
标题:“B”
电池:所有品牌
这很正常,因为我的for循环中遗漏了一些东西

我如何用swift语法更改我的
for loop
,以便它可以将以“A”开头的每个品牌名称附加在字母“A”中

所以它会显示这样的东西

标题:“A”
电池:以A开头的品牌
标题:“B”
单元格:以B开头的品牌
等等

非常感谢


//迭代每个标题名
对于self.sortedLetters中的startNames{
//筛选出名称以startNames开头的品牌
//在这里,我还按品牌名称对其进行了分类。
让brands=self.sortedBrands.filter(){$0.name.hasPrefix(startNames)}.sorted(按:{$0.name<$1.name})
self.objectBrands.append(对象(sectionName:startNames,sectionObjects:brands))
}


//迭代每个标题名
对于self.sortedLetters中的startNames{
//筛选出名称以startNames开头的品牌
//在这里,我还按品牌名称对其进行了分类。
让brands=self.sortedBrands.filter(){$0.name.hasPrefix(startNames)}.sorted(按:{$0.name<$1.name})
self.objectBrands.append(对象(sectionName:startNames,sectionObjects:brands))
}

为您的章节使用字典,其中键是章节标题的字母,值是包含该章节模型的数组。您需要单独对密钥进行排序(或者只需预计算并将已排序的密钥存储在一个数组中;如果需要稀疏字典,则每次数据更改时都需要执行此操作(即,仅显示包含项的节的标题,或者仅显示一次,如果要显示所有标题,而不管它们是否为空)

var节:[字符串:[ModelType]]=[:]
func tableView(tableView:UITableView,titleForHeaderInSection:Int)->String{
返回sortedSectionKeys[section]//sortedSectionKeys=sections.keys.sorted{$0.0<$0.1}
}
func numberOfSections(在tableView:UITableView中)->Int{
返回节.keys.count
}
func sectionIndexTitles(对于tableView:UITableView)->[字符串]{
返回已分类的分区键
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回段[sortedSectionKeys[section]]?。计数??0
}

为您的节使用字典,其中键是节标题的字母,值是包含该节模型的数组。您需要单独对键进行排序(或者只需预计算并将已排序的键存储在数组中;如果需要稀疏字典,则每次数据更改时都需要这样做(即,仅显示包含项目的节的标题,或者仅显示一次,如果您希望显示所有标题,而不管它们是否为空)

var节:[字符串:[ModelType]]=[:]
func tableView(tableView:UITableView,titleForHeaderInSection:Int)->String{
返回sortedSectionKeys[section]//sortedSectionKeys=sections.keys.sorted{$0.0<$0.1}
}
func numberOfSections(在tableView:UITableView中)->Int{
返回节.keys.count
}
func sectionIndexTitles(对于tableView:UITableView)->[字符串]{
返回已分类的分区键
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回段[sortedSectionKeys[section]]?。计数??0
}

您可以按第一个字母过滤您的
分类器
数组,如下所示:

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

例如,这将仅显示带有“a”(小写或大写)的品牌。您可以使用任何一个变量替换该“a”,以确定您所处的区域。

您可以按如下第一个字母筛选您的
sortedletter
数组:

sortedLetters.filter({ $0.substring(to: $0.index($0.startIndex, offsetBy: 1)).lowercased() == "a" })
例如,这将只显示带有“a”(小写或大写)的品牌。您可以使用任何变量替换该“a”,以确定您所处的区域