gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:40:53 25 4
gpt4 key购买 nike

我想将部分标题显示为一月、二月、三月(根据来自 api 的数据),而不是显示一月、三月、二月。

我从后端 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 数据源和委托(delegate)是

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

}

最佳答案

根据评论,您可以将 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"

希望对你有帮助

关于ios - 我想制作带有动态行的动态部分的 Uitableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52277254/

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