[英]Populate UITableView section header title since section == 2?
我有一個來自名為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]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.