[英]How to display the count of a filtered array of objects
我有一個對象數組,代表幾個部分。 每個部分包含許多項目。 我設法過濾所有部分以在UITableView
顯示所需的項目,但是我無法從我的部分中獲得正確的項目數。 每次過濾數組后,我都會得到原始的項目計數。 我的控制台說: Number of items in each section: [9, 6]
UITableView
Number of items in each section: [9, 6]
但實際上我的UITableView
中只有[ UITableView
]。 如何反映適量的項目以繼續使用數組? 這是我的代碼:
// 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.
}
}
感謝您閱讀本文!
如您所知, filter
實際上並未刪除項目。 它只是返回一個刪除了某些項目的新數組。 因此,當您執行以下嵌套過濾器時:
checklistItem.filter { $0.checklistItems.filter { ... }.count != 0 }
它將返回checklistItem
但刪除了一些元素,然后將此結果分配給checklistItem
,從而對其進行更改。 但是請注意, checklistItem
的內部數組(即checkListItems
)沒有更改!
內部filter
只返回一個新數組(並且實際上不修改數組本身)。
我建議你forEach
的事情checklistItem
, removeAll
滿足標准,則刪除所有的checklistItems
是使用另一種空removeAll
:
checklistItem.forEach { $0.checklistItems.removeAll { !$0.showTrailer || $0.showVehicle } }
checklistItem.removeAll { $0.count == 0 }
最后,您不需要額外的過濾器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.