我有一个来自名为 firstData
的服务器的响应数组,它有 12 个元素。里面有一堆字典,我做了一些过滤器将其分成 3 个不同的数组,如下所示:
//filter to get all type Switch into one array.
self.dataSwitchOnly = self.firstData.filter {
$0.type.rawValue == "Switch"
}// 7 elements
//looping dataSwitchOnly to get all shop type.
for i in 0..<self.dataSwitchOnly.count {
if (self.dataSwitchOnly[i].search_key == "shop_type" || self.dataSwitchOnly[i].search_key == "is_certified"){
self.shopTypeArr.append(self.dataSwitchOnly[i])
}
}// 2 elements
//filter original data to get all elements are not have type Switch.
self.dataAfterFilterSwitch = self.firstData.filter {
$0.type.rawValue != "Switch"
}//5 elements
我的 UITableView numberOfSections
像这样:
//return filtered array which is not type `Switch` then + 2 more static section 0 and 1.
return dataAfterFilterSwitch.count + 2
并且因为 shopTypeArr
和 dataSwitchOnly
没有相同的名称。因此,我必须对它们的标题标题进行硬编码,第 0 部分是“快速过滤器”,第 1 部分是“商店类型”。
现在我想显示从第 2 部分到 tableView 末尾的 dataAfterFilterSwitch
。但是,当我设置 dataAfterFilterSwitch[section]
来获取属性 name
时,我收到错误 index out of range
。也许我的问题让你们很困惑,但我可以提供更多代码来解决这个问题。
我的代码逻辑哪里有问题?
我的viewForHeaderInSection
:
let itemFilter = self.dataAfterFilterSwitch[section-2]
// header.isCollapse = collapse[section]
// header.delegate = self
if section == 0 {
let data = SectionHeaderModel(sectionTitle: "Quick Filter".uppercased())
if header.isCollapse == 0 {
header.sectionComponents?.bind(data: data, state: .collapse, imageState: ImageDataType.image(UIImage(named: "icon-24-chevron-bottom")!), target: self, selector:nil)
}else {
header.sectionComponents?.bind(data: data, state: .collapse, imageState: ImageDataType.image(UIImage(named: "icon-24-chevron-top")!), target: self, selector:nil)
}
return header
}else if section == 1 {
let data = SectionHeaderModel(sectionTitle: "Shop Type".uppercased())
if header.isCollapse == 0 {
header.sectionComponents?.bind(data: data, state: .collapse, imageState: ImageDataType.image(UIImage(named: "icon-24-chevron-bottom")!), target: self, selector:nil)
}else {
header.sectionComponents?.bind(data: data, state: .collapse, imageState: ImageDataType.image(UIImage(named: "icon-24-chevron-top")!), target: self, selector:nil)
}
return header
}else {
let data = SectionHeaderModel(sectionTitle:(itemFilter.name)!.uppercased())
if header.isCollapse == 0 {
header.sectionComponents?.bind(data: data, state: .collapse, imageState: SImageDataType.image(UIImage(named: "icon-24-chevron-bottom")!), target: self, selector:nil)
}else {
header.sectionComponents?.bind(data: data, state: .collapse, imageState: ImageDataType.image(UIImage(named: "icon-24-chevron-top")!), target: self, selector:nil)
}
return header
}
最佳答案
由于您在 tableView 中添加了两个硬代码部分,并且您必须在这些部分之后显示数组的数据。您必须像这样从数组中检索数据 dataAfterFilterSwitch[section - 2]
关于arrays - 填充 UITableView 节标题标题,因为节 == 2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59155194/