代码之家  ›  专栏  ›  技术社区  ›  iHarshil l0gg3r

如何返回第节中的行数?

  •  1
  • iHarshil l0gg3r  · 技术社区  · 7 年前

    我真的需要帮助,因为我是新来的斯威夫特。我正面临一个找不到归路的问题 行数截面

    脚本:

    • 我在TabLVIEW中有3个数字节,我正在从 分段数组 ,具体如下:

      {
                          "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
                          ]
                      }
                  ]
              }
      

    使用上面的数组,我可以返回如下数量的节:

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

    但是,如何返回 行数截面 使用 分段数组 ??

    笔记

    我想要 行数 “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中的方法:

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

    let headers=swiftyjsonvar[“消息”][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)
        }
    

    我知道这可能是愚蠢的,这太容易了,但不知怎的,我是一个新的结巴。 任何帮助都将不胜感激,谢谢!

    3 回复  |  直到 7 年前
        1
  •  3
  •   Anbu.Karthik    7 年前
    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
     }   
    }
    
        2
  •  2
  •   Faysal Ahmed    7 年前
    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
         }
    }
    
        3
  •  1
  •   Arnab    7 年前

    试试这个:

    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
                } 
    }