Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何返回节中的行数?_Ios_Arrays_Swift_Uitableview - Fatal编程技术网

Ios 如何返回节中的行数?

Ios 如何返回节中的行数?,ios,arrays,swift,uitableview,Ios,Arrays,Swift,Uitableview,我真的需要帮助,因为我是斯威夫特的新手。我面临一个无法返回numberOfRowsInSection 场景: 我在tableview中有3个小节,我将从sectionArray返回,如下所示: { "mbsdcie6_2_1": "Incomer 132 KV Parameters", "mbsdcie6_2_2": [ 3,

我真的需要帮助,因为我是斯威夫特的新手。我面临一个无法返回numberOfRowsInSection

场景:

  • 我在tableview中有3个小节,我将从sectionArray返回,如下所示:

    {
                        "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
                        ]
                    }
                ]
            }
    
使用上面的数组,我可以像这样返回NumberOfSection:

func numberOfSections(in tableView: UITableView) -> Int {
    return self.sectionArray.count
}
var sectionArray = [Dictionary<String, Any>]()

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

        print(sectionArray)
    }
但是,如何使用节数组返回节中的行数

注意:

我想要节数组的中的行数“mbsdcie6_2_2”

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中的:

//它来自另一个Web服务,所以我将其添加到userdefaults中

let headers=swiftyJSonVar[“message”][0][“mbsdcie6”][“mbsdcie6_2”]。arrayObject

kUserDefault.set(headers, forKey: kHeadersArray)
//在我的另一个ViewController中,我检索它的方式如下:

func numberOfSections(in tableView: UITableView) -> Int {
    return self.sectionArray.count
}
var sectionArray = [Dictionary<String, Any>]()

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

        print(sectionArray)
    }
var sectionArray=[Dictionary]()
覆盖函数视图将出现(uo动画:Bool){
sectionArray=kUserDefault.array(forKey:kHeadersArray)as![字典]
打印(分区数组)
}
我知道这可能很傻,而且太简单了,但不知怎么的,我还是个新手,所以我很累。 任何帮助都将不胜感激,谢谢

试试这个:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if sectionArray.indices.contains(section),
        let sectionContent = sectionArray[section]["mbsdcie6_2_2"] as? [Int] {
        return sectionContent.count
     } else {
        return 0
     }
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let singleSection = self.sectionArray[section]
            if let rows = singleSection["mbsdcie6_2_2"] as? [Int] {
                      return rows.count
            } else {
                       return 0
            } 
}
试试这个:

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

仅存储所有三个数组而不是通过计数数组值请添加代码如何在sectionArray中添加数组数据看起来
sectionArray
实际上是一个字典。仅存储所有三个数组而不是通过计数数组值请添加代码如何在sectionArray中添加数组数据看起来
sectionArray
实际上是一个字典字典,对不起!我没有否决投票。但我要这把钥匙的行数“mbsdcie6_2_2”。我记下了你。您的解决方案完全错误。请立即查看。。。我没有看完整个问题@脱毛!我没有否决投票。但我要这把钥匙的行数“mbsdcie6_2_2”。我记下了你。您的解决方案完全错误。请立即查看。。。我没有看完整个问题@DeDeDenovathanks,但它给出了如下警告:从“[String:Any]”到“[String:Any]”的条件转换总是成功的。然而,代码是有效的!谢谢,但它给出了如下警告:从“[String:Any]”到“[String:Any]”的条件转换总是成功的。然而,代码是有效的!