我有一个对象数组,这些对象代表几个小节。每个部分包含许多项目。我设法过滤了所有部分,以在UITableView
中显示所需的项目,但是我无法从我的部分中获得正确的项目数。每次过滤数组后,我都会得到原始的项目计数。
我的控制台说:Number of items in each section: [9, 6]
,但实际上我的UITableView
只有[6,5]。如何反映适量的项目以继续使用数组?
这是我的代码:
// 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(), batteryUnitAndFridge()]
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: [])!)
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: 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)
}
private class func batteryUnitAndFridge() -> ChecklistItemSection {
var checklistItems = [ChecklistItem]()
checklistItems.append(ChecklistItem(templateID: 38, lineID: 31, poolID: 38, descript: "Battery is held securely in place by the correct means (not cables)", showVehicle: true, showTrailer: true, highlight: false, pg9: true, imagesPath: [])!)
checklistItems.append(ChecklistItem(templateID: 38, lineID: 32, poolID: 39, descript: "Battery cables and pins secure", showVehicle: true, showTrailer: true, highlight: false, pg9: false, imagesPath: [])!)
checklistItems.append(ChecklistItem(templateID: 38, lineID: 33, poolID: 40, descript: "Battery is not leaking", showVehicle: true, showTrailer: true, highlight: false, pg9: false, imagesPath: [])!)
checklistItems.append(ChecklistItem(templateID: 38, lineID: 34, poolID: 38, descript: "Battery is held securely in place by the correct means (not cables)", showVehicle: true, showTrailer: true, highlight: false, pg9: true, imagesPath: [])!)
checklistItems.append(ChecklistItem(templateID: 38, lineID: 35, poolID: 39, descript: "Battery cables and pins secure", showVehicle: false, showTrailer: true, highlight: false, pg9: false, imagesPath: [])!)
checklistItems.append(ChecklistItem(templateID: 38, lineID: 35, poolID: 39, descript: "Battery is not leaking", showVehicle: true, showTrailer: false, highlight: false, pg9: false, imagesPath: [])!)
return ChecklistItemSection(named: "Section 3", checklistItems: checklistItems)
}
}
class ChecklistItem {
var showVehicle: Bool
var showTrailer: Bool
}
// MARK: VC
class ChecklistVC: UIViewController {
lazy var checklistItem: [ChecklistItemSection] = { return ChecklistItemSection.checklistItemSections() }()
override func viewDidLoad() {
super.viewDidLoad()
filterQuestions()
}
func filterQuestions() {
// Show only the questions available for Trailer
if vehicleRegistrationNumber.isBlank {
checklistItem.forEach {
$0.checklistItems.forEach {
if $0.showVehicle {
$0.showVehicle = false
}
}
}
checklistItem = checklistItem.filter { $0.checklistItems.filter { $0.showTrailer && !$0.showVehicle }.count != 0 }.filter {$0.checklistItems.count > 0}
}
print("Number of items in each section: \(checklistItem.map {$0.checklistItems.count})") // I get [9, 6] every time. But it should display [6, 5] reflecting the items after filtering.
}
}
谢谢阅读!
答案 0 :(得分:2)
如您所知,filter
实际上并未删除项目。它只是返回一个删除了某些项目的新数组。因此,当您执行这样的嵌套过滤器时:
checklistItem.filter { $0.checklistItems.filter { ... }.count != 0 }
它将返回checklistItem
,但删除了某些元素,然后将此结果分配给checklistItem
,从而对其进行更改。但是请注意,checklistItem
(即checkListItems
)的内部数组没有更改!
内部filter
也只返回一个新数组(并且实际上不修改数组本身)。
我建议您在forEach
,checklistItem
中满足条件的removeAll
事物中,然后通过使用另一个{{1} }:
checklistItems
您最后不需要多余的过滤器。