我想用带有动态行的动态部分制作Uitableview

问题描述 投票:1回答:2

我想将部分标题显示为1月,2月,3月(根据数据来自api),而不是显示1月,3月,2月。

我从后端api获取此数据。

wholeDic : NSDictionary = {

January =     (
            {
        date = "20-jan-18";
        subtitle = "Only four days remaining";
        title = "School fees of August, 2018School fees of August, 2018";
    },
            {
        date = "21-jan-18";
        subtitle = Holi;
        title = "Holiday on third march";
    }
);
february =     (
            {
        date = "20-feb-18";
        subtitle = "Paricipate in this activity";
        title = "Annual function will be held in feb,2018";
    },
            {
        date = "20-12-18";
        subtitle = "Holiday issue by Govt. of India";
        title = "Bharat Band";
    }
);
march =     (
            {
        date = "20-feb-18";
        subtitle = "Paricipate in this activity";
        title = "Annual function will be held in feb,2018";
    },
            {
        date = "20-feb-18";
        subtitle = "Paricipate in this activity";
        title = "Annual function will be held in feb,2018";
    }
);}

现在我已经获取了“key”并添加了数组

for (key, value) in wholeDic{
   sectionTitleArray.add(key)
   print(sectionTitleArray)
}

当我打印sectionTitleArray而不是控制台显示January, March, February而不是显示January, February, March

我知道Dictionary是一个无序的集合,但我想知道如何按顺序获取密钥?

我的UitableView数据源和代理是

func numberOfSections(in tableView: UITableView) -> Int{

    return sectionTitleArray.count
}

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

    let sectionTitle : String = sectionTitleArray.object(at: section) as! String
    let sectiondes:NSArray = wholeDic.object(forKey: sectionTitle) as! NSArray
    return sectiondes.count

}

This is my tableView .Everything is working fine but I want to display month like JANUARY,FEBRUARY,MARCH as per api data.

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellIdentifier = "cell"
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! frameNboundTableViewCell
    let sectionTitle : String = sectionTitleArray.object(at: indexPath.section) as! String
    let contentArr : NSArray = wholeDic.object(forKey: sectionTitle) as! NSArray
    let contentDic : NSDictionary = contentArr.object(at: indexPath.row) as! NSDictionary
    print(contentDic)
    cell.titleLbl.text = contentDic.value(forKey: "title") as? String
    cell.titleLbl.numberOfLines = 0
    return cell

}
ios swift uitableview nsdictionary indexpath
2个回答
0
投票

根据注释,您可以将api响应更改为数组,只需使用索引进行访问即可。 json响应将如下所示

    {
  "data" : [
    {
      "month" : "January",
      "details" : [
        {
          "date" : "20-jan-18",
          "subtitle" : "Only four days remaining",
          "title" : "School fees of August, 2018School fees of August, 2018"
        },
        {
          "date" : "21-jan-18",
          "subtitle" : "Holi",
          "title" : "Holiday on third march"
        }
      ]
    },
    {
      "month" : "february",
      "details" : [
        {
          "date" : "20-feb-18",
          "subtitle" : "Paricipate in this activity",
          "title" : "Annual function will be held in feb,2018"
        },
        {
          "date" : "20-12-18",
          "subtitle" : "Holiday issue by Govt. of India",
          "title" : "Bharat Band"
        }
      ]
    },
    {
      "month" : "march",
      "details" : [
        {
          "date" : "20-feb-18",
          "subtitle" : "Paricipate in this activity",
          "title" : "Annual function will be held in feb,2018"
        },
        {
          "date" : "20-feb-18",
          "subtitle" : "Paricipate in this activity",
          "title" : "Annual function will be held in feb,2018"
        }
      ]
    }
  ]
}

现在,您可以从json数组访问第一个对象,然后检索相应的键并显示它。它会是这样的

let object = response.data![index]
object.month // "January"

希望能帮助到你


0
投票

你应该做第一个字母大写:

extension String {
    func capitalizingFirstLetter() -> String {
        return prefix(1).capitalized + dropFirst()
    }

    mutating func capitalizeFirstLetter() {
        self = self.capitalizingFirstLetter()
    }
}

部分标题:

let sectionTitleStr = sectionTitleArray.object(at: indexPath.section) as! String
let sectionTitle : String = sectionTitleStr.capitalizingFirstLetter()
© www.soinside.com 2019 - 2024. All rights reserved.