ios - 如何返回 numberOfRowsInSection?

标签 ios arrays swift uitableview

我真的需要帮助,因为我是 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/

相关文章:

ios - 如何使符合已定义协议(protocol)的 UITableViewCells 出队

javascript - 尝试从索引中的状态数组进行拼接,但最终却从第一个索引到最后一个索引进行拼接

swift - Parse 和 Swift 的可选类型

ios - 关闭 UITableViewRowAction

ios - UIScrollView 中的 UIView 没有出现

objective-c - 如何访问另一个 Controller / View (iOS)

iOS Swift - 如何获取本地和远程视频的宽高比?

java - 在对象数组中找不到符号

javascript - 从混合字符数组中仅检索整数值

ios - 如何在文件管理器中保存多个图像