gpt4 book ai didi

ios - 如何返回 numberOfRowsInSection?

转载 作者:搜寻专家 更新时间:2023-11-01 05:55:03 26 4
gpt4 key购买 nike

我真的需要帮助,因为我是 Swift 的新手。我遇到一个问题,我找不到返回 numberOfRowsInSection

的方法

场景:

  • 我从 sectionArray 返回的 TableView 中有 3 个 numberOfSections,如下所示:

    {
    "mbsdcie6_2_1": "Incomer 132 KV Parameters",
    "mbsdcie6_2_2": [
    3,
    6,
    4,
    5,
    8,
    30,
    31,
    7,
    18,
    29,
    1
    ]
    },
    {
    "mbsdcie6_2_1": "SMS2 Parameters",
    "mbsdcie6_2_2": [
    9,
    10,
    11,
    12,
    13,
    14
    ]
    },
    {
    "mbsdcie6_2_1": "SMS 1 Parameters",
    "mbsdcie6_2_2": [
    15,
    17
    ]
    }
    ]
    }

使用上面的数组,我可以像这样返回 numberOfSections:

func numberOfSections(in tableView: UITableView) -> Int {
return self.sectionArray.count
}

但是,如何使用 sectionArray 返回 numberOfRowsInSection ??

注意:

我想要 numberOfRows 来自 "mbsdcie6_2_2" key of sectionArray

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

if section == 0 {
return "numberOfRows" // What to return here?
}
if section == 1 {
return "numberOfRows"
}
if section == 2 {
return "numberOfRows"
}
return 0
}

编辑 1

这就是我将数据添加到 sectionArray 的方式:

//它来自另一个 webservice,所以我将它添加到 userdefaults。

let headers = swiftyJSonVar["message"][0]["mbsdcie6"]["mbsdcie6_2"].arrayObject

kUserDefault.set(headers, forKey: kHeadersArray)

//在我的另一个 ViewController 中,我是这样检索它的:

var sectionArray = [Dictionary<String, Any>]()

override func viewWillAppear(_ animated: Bool) {
sectionArray = kUserDefault.array(forKey: kHeadersArray) as! [Dictionary<String, Any>]

print(sectionArray)
}

我知道这可能很愚蠢,而且太简单了,但由于我是新手,不知何故我被困住了。任何帮助将不胜感激,谢谢!

最佳答案

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if !sectionArray.isEmpty {
let sectionContent = sectionArray[section]["mbsdcie6_2_2"] as? [Int]
return sectionContent.count
} else {
return 0
}
}

关于ios - 如何返回 numberOfRowsInSection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50982224/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com