首页 > 解决方案 > 根据 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 
}

标签: jsonswift

解决方案


如果您的枚举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
}

推荐阅读