json - 根据 JSON 中的枚举显示 tableview 部分中的行数
问题描述
我有从 JSON 文件中检索数据的 tableview。我的目标是在我的 tableview 中创建四个部分 [“Course”、“Guide”、“Article”和“Interview”]。如果一个对象的类型称为“课程”,那么我希望该对象显示在“课程”部分等等。
到目前为止,我有四个部分和准备显示的数据,但我不知道如何计算每个类型对象有多少对象并将它们添加到部分函数中的行数。
我的 json 文件如下所示:
{
"resources": [
{
"title": "Cooking with Me",
"url": "https://google.com",
"type": "course",
"date": "2020",
"author": "Chef A"
},
{
"title": "Daily Cooking",
"url": "https://google.com",
"type": "guide",
"date": "2020",
"author": "Chef B"
},
{
"title": "Simple Recipes",
"url": "https://google.com",
"type": "guide",
"date": "2020",
"author": "Chef C"
}
]
}
// 模型
struct Resources: Codable {
let resources: [Resource]
}
// MARK: - Resource
struct Resource: Codable {
let title: String
let url: String
let type: TypeEnum
let date, author: String
}
enum TypeEnum: String, Codable {
case article = "article"
case course = "course"
case guide = "guide"
case interview = "interview"
}
// 视图控制器
class ResourcesController: UIViewController, UITableViewDelegate, UITableViewDataSource {
//MARK: - Properties
var items = [Resource]()
//MARK: - IBOutlets
@IBOutlet weak var resourceTable: UITableView!
// MARK: - View Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
retrieveDate()
}
//MARK: - Methods
func retrieveDate() {
do {
let data = try Data(contentsOf: Bundle.main.url(forResource: "freeResources", withExtension: "json")!)
let decoded = try JSONDecoder().decode(Resources.self, from: data)
items = decoded.resources
resourceTable.reloadData()
} catch { print(error) }
}
//MARK: - Tableview Data Source
func numberOfSections(in tableView: UITableView) -> Int {
return 4
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 {
return "Courses"
} else if section == 1 {
return "Guides"
} else if section == 2 {
return "Articles"
} else {
return "Interview"
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "sourceCell", for: indexPath) as! SourceCell
cell.authorLabel.text = items[indexPath.row].author
cell.titleLabel.text = items[indexPath.row].title
return cell
}
解决方案
如果您的枚举TypeEnum
中的项目与您想要的部分的顺序相同(您似乎已经拥有),那么您可以使枚举符合,CaseIterable
这意味着您可以获得所有项目的数组并使用它。
enum TypeEnum: String, Codable, CaseIterable {
case article = "article"
case course = "course"
case guide = "guide"
case interview = "interview"
}
现在你可以在一行中得到计数
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.filter { $0.type == TypeEnum.allCases[section]}.count
}
并且在创建单元格时
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "sourceCell", for: indexPath) as! SourceCell
let type = TypeEnum.allCases[indexPath.section]
let item = items.filter { $0.type == type }[indexPath.row]
cell.authorLabel.text = item.author
cell.titleLabel.text = item.title
return cell
}
您还可以更改节数功能以获得更多未来证明代码
func numberOfSections(in tableView: UITableView) -> Int {
return TypeEnum.allCases.count
}
推荐阅读
- rest - Flutter web:如何连接到一个 REST API 或 localhost
- javascript - 当我尝试在 React 中映射和渲染数据时,应用程序崩溃了
- java - HTTPClient ConnectionKeepAliveStrategy 未按预期工作
- angular - 错误错误:未捕获(承诺):TypeError:无法读取Angular和谷歌地图自动完成中未定义的属性'nativeElement'
- c# - 在页面之间保留照片数据的最佳方式是什么?
- javascript - 如何在 Object.keys 中设置/编辑变量
- javascript - 生成 powerpoint 时使用 RED X 代替图像
- python - 我想做出随机选择,但它的值有权重
- amazon-ecs - 如何在 CodePipeline 中处理 ECS 部署以更改任务定义
- python - 即使已安装,也无法在 jupyter notebbok 中导入 numpy 库