我有一个UITableView
,由一个对象数组填充(每个对象都是一个节)。在此对象数组中,每个对象只有几个项目(带有说明和其他属性的问题)。
目前,我在UITableView
中隐藏了所有不需要的问题。但是这种做法不可行,因此我想重构并过滤问题以仅从每个部分中获取必需的问题。
所以我想有一个带有部分的数组,其中每个部分的项目都被过滤了。
要求是:
item.showTrailer == false && item.showVehicle == true
。
这是我的代码:
// MARK: MODEL
class ChecklistItemSection {
var name: String // name of the section
var checklistItems: [ChecklistItem] // all items from Checklist
init(named: String, checklistItems: [ChecklistItem]) {
self.name = named
self.checklistItems = checklistItems
}
class func checklistItemSections() -> [ChecklistItemSection] {
var allSections = [vehicleCheck(), viewingScreen() ]
for (index, section) in allSections.enumerated() {
if(section.checklistItems.count < 1) {
allSections.remove(at: index)
}
}
return allSections
}
// Private methods
private class func vehicleCheck() -> ChecklistItemSection {
var checklistItems = [ChecklistItem]()
checklistItems.append(ChecklistItem(templateID: 109, lineID: 3, poolID: 10, descript: "Tyres - Wear/Damage/Bulges/Cuts/Flat tyres", showVehicle: false, showTrailer: true, highlight: false, pg9: false, imagesPath: [])!)
checklistItems.append(ChecklistItem(templateID: 109, lineID: 4, poolID: 22, descript: "Vehicle and trailer coupling: undamaged and safety locking device working", showVehicle: true, showTrailer: false, highlight: true, pg9: true, imagesPath: [])!)
checklistItems.append(ChecklistItem(templateID: 109, lineID: 7, poolID: 20, descript: "Exhaust - Condition/Emission (Excess smoke)", showVehicle: true, showTrailer: false, highlight: false, pg9: false, imagesPath: [])!)
checklistItems.append(ChecklistItem(templateID: 109, lineID: 3, poolID: 10, descript: "Tyres - Wear/Damage/Bulges/Cuts/Flat tyres", showVehicle: true, showTrailer: false, highlight: false, pg9: false, imagesPath: [])!)
checklistItems.append(ChecklistItem(templateID: 109, lineID: 4, poolID: 22, descript: "Vehicle and trailer coupling: undamaged and safety locking device working", showVehicle: true, showTrailer: true, highlight: true, pg9: true, imagesPath: [])!)
checklistItems.append(ChecklistItem(templateID: 109, lineID: 7, poolID: 20, descript: "Exhaust - Condition/Emission (Excess smoke)", showVehicle: false, showTrailer: true, highlight: false, pg9: false, imagesPath: [])!)
return ChecklistItemSection(named: "Section 1", checklistItems: checklistItems)
}
private class func viewingScreen() -> ChecklistItemSection {
var checklistItems = [ChecklistItem]()
checklistItems.append(ChecklistItem(templateID: 38, lineID: 28, poolID: 16, descript: "Windscreen Wipers & Washers are they effective?", showVehicle: true, showTrailer: false, highlight: false, pg9: false, imagesPath: [])!)
checklistItems.append(ChecklistItem(templateID: 38, lineID: 28, poolID: 16, descript: "Water Level - In cab indicator", showVehicle: true, showTrailer: false, highlight: false, pg9: false, imagesPath: [])!)
return ChecklistItemSection(named: "Section 2", checklistItems: checklistItems)
}
}
class ChecklistItem{
var showVehicle: Bool
var showTrailer: Bool
}
// MARK: VC
class ChecklistViewController: UIViewController {
lazy var checklistItem: [ChecklistItemSection] = { return ChecklistItemSection.checklistItemSections() }()
var filteredQuestions: [ChecklistItemSection] = []
override func viewDidLoad() {
super.viewDidLoad()
filteredQuestions = All sections where the questions have showVehicle == true && showTrailer == false
}
}
感谢阅读。
答案 0 :(得分:1)
您可以尝试
filteredQuestions = checklistItem.filter { $0.checklistItems.filter { !$0.showTrailer && $0.showVehicle }.count != 0 }
编辑:
checklistItem.forEach {
$0.checklistItems.forEach {
if $0.showTrailer {
$0.showTrailer = false
}
}
}
答案 1 :(得分:0)
您可以使用以下方法过滤数组:
您将获得每个CheckListItem的filteredQuestions。
override func viewDidLoad() {
super.viewDidLoad()
for item in checklistItem {
filteredQuestions = item.filter{ $0.showTrailer == false && $0.showVehicle == true }
}
}
答案 2 :(得分:0)
您需要重新创建版块,并过滤其清单。
format