ios - 如何显示过滤后的对象数组的计数

标签 ios swift

我有一个代表几个部分的对象数组。每个部分包含许多项目。我设法过滤了所有部分以在我的 UITableView 中显示需要的项目,但我无法设法从我的部分中获得正确的项目数。每次过滤数组后,我都会得到原始的项目数。 我的控制台显示:每个部分的项目数:[9, 6] 但实际上在我的 UITableView 中只有 [6, 5]。我怎样才能反射(reflect)出正确数量的项目来使用我的数组 forward ? 这是我的代码:



// 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 }

最后不需要额外的过滤器。

关于ios - 如何显示过滤后的对象数组的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56583523/

相关文章:

swift - AVAudioPlayerNode - 获取播放器状态?

css - 使用 fx-layout ="column"的 Safari IOS 的 Flex 布局显示问题

ios - swift 如何更简单的实现copy方法?

ios - 未定义不是对象(评估 RNRandomBytes.seed)

swift - 从类中调用函数

ios - 打包 iOS 应用程序后推送通知停止工作

swift - 指示tableView的indexPath何时为最后一个

ios - 如何在xcode中更改scheme的容器?

iphone - 从 UIImagePickerController 抓取视频的第一帧?

ios - 下载图像时检查 Nil 后变量显示 Nil