我有一个名为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部分中是商店类型。
现在,我想显示我的dataAfterFilterSwitch
从第2节到tableView的结尾。但是,当我将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
}
答案 0 :(得分:0)
由于您在tableView中添加了两个硬代码部分,因此必须在这些部分之后显示数组的数据。您必须像这样dataAfterFilterSwitch[section - 2]