【问题标题】:Show TableView Section Headers and Rows ios swift显示 TableView 部分标题和行 ios swift
【发布时间】:2017-11-17 05:57:42
【问题描述】:
我有一个包含对象的数组,我想在标题中显示组名,但在数组中某些组具有相同的名称,现在我想为同一组显示单个标题并创建行。
[
       {
         "primary_dark_color": "#0d6d9f",
         "Groups": "A",
         "primary_color": "#03a9f4"
       },
       {
         "primary_dark_color": "#0d6d9f",
         "Groups": "B",
         "primary_color": "#03a9f4"
        },
        {
         "primary_dark_color": "#0d6d9f",
         "Groups": "A",
         "primary_color": "#03a9f4"
        },
        {
         "primary_dark_color": "#0d6d9f",
         "Groups": "B",
         "primary_color": "#03a9f4"
       }]

【问题讨论】:

    标签: ios uitableview swift3


    【解决方案1】:

    要解决您的问题,您需要使用谓词。使用谓词,您可以创建包含相同组的字典组(数组)

    试试下面的代码。

    let array = [
        [
            "primary_dark_color": "#0d6d9f",
            "Groups": "A",
            "primary_color": "#03a9f4"
            ],
        [
            "primary_dark_color": "#0d6d9f",
            "Groups": "B",
            "primary_color": "#03a9f4"
            ],
        [
            "primary_dark_color": "#0d6d9f",
            "Groups": "A",
            "primary_color": "#03a9f4"
            ],
        [
            "primary_dark_color": "#0d6d9f",
            "Groups": "B",
            "primary_color": "#03a9f4"
            ]]
    
        var arrGroups = [String]()
        for dict in array {
    
            if !arrGroups.contains(dict["Groups"]!) {
    
                arrGroups.append(dict["Groups"]!)
            }
        }
        var arrTmp = [Array<Dictionary<String, String>>]()
        for i in 0..<arrGroups.count {
    
            var tmp:Array = array
            tmp = tmp.filter { ($0["Groups"]!).range(of: arrGroups[i], options: [.diacriticInsensitive, .caseInsensitive]) != nil }
            arrTmp.append(tmp)
        }
        print(arrTmp)
    

    arrTmp 中的每个数组都有包含相同组的字典

    所以你的numberOfSections = arrTmp.count

    还有numberOfRowsInSection = arrTmp[section].count

    arrTmp 会有

    [[["primary_color": "#03a9f4", "primary_dark_color": "#0d6d9f", "Groups": "A"], ["primary_color": "#03a9f4", "primary_dark_color": "#0d6d9f", "Groups": "A"]], [["primary_color": "#03a9f4", "primary_dark_color": "#0d6d9f", "Groups": "B"], ["primary_color": "#03a9f4", "primary_dark_color": "#0d6d9f", "Groups": "B"]]]
    

    【讨论】:

    • 谢谢@Priya 你能告诉我数组值吗
    猜你喜欢
    • 2015-07-20
    • 2020-06-23
    • 1970-01-01
    • 2020-09-16
    • 2020-04-24
    • 1970-01-01
    • 2019-05-10
    • 2020-03-15
    • 2017-10-11
    相关资源
    最近更新 更多