ios - 如何过滤 UITableView 中的所有部分以获取想要的项目

标签 ios arrays swift filter

我有一个由对象数组填充的 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/

相关文章:

ios - Alamofire 没有评估和发送客户端证书

java - 二维阵列扫雷器周围的地雷

javascript - 移动二维数组中的行和列 - Javascript

iOS:如何在代码中的表格单元格中为文本字段添加约束

objective-c - "unrecognized selector"尝试监听 UIApplicationDidReceiveMemoryWarningNotification 时

arrays - 将 ColdFusion session 转换为数组(日期)正则表达式问题

ios - 推文单元格 swift 上的操作按钮

ios - 找不到合并 @Published - Xcode11 Beta 5(11M382q)

ios - 动画 map 到位置不工作谷歌地图 iOS

ios - SVProgressHUD 如何延迟指定的时间