我有一个由对象数组填充的 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
}
}
感谢阅读本文。
最佳答案
你可以试试
filteredQuestions = checklistItem.filter { $0.checklistItems.filter { !$0.showTrailer && $0.showVehicle }.count != 0 }
编辑:
checklistItem.forEach {
$0.checklistItems.forEach {
if $0.showTrailer {
$0.showTrailer = false
}
}
}
关于ios - 如何过滤 UITableView 中的所有部分以获取想要的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56578198/