gpt4 book ai didi

swift - 需要按 ObjectBulletin.id 对 tableview 进行排序

转载 作者:行者123 更新时间:2023-11-28 14:51:59 25 4
gpt4 key购买 nike

我需要按 ObjectBulletin 中的 id 对我的 tableview 数据进行排序。我无法弄清楚如何对 id 进行排序并正确反射(reflect)数据。

我的结构

struct BulletinHelper: Codable {
let limit: Int?
let offset: Int?
let objects: [ObjectBulletin]?
let totalCount: Int?

enum CodingKeys: String, CodingKey {
case limit = "limit"
case offset = "offset"
case objects = "objects"
case totalCount = "total_count"
}
}

struct ObjectBulletin: Codable {
let id: Int?
let portalID: Int?
let name: String?
let size: Int?
let height: JSONNullBulletin?
let width: JSONNullBulletin?
let encoding: JSONNullBulletin?
let type: String?
let objectExtension: String?
let cloudKey: String?
let s3URL: String?
let friendlyURL: String?
let altKey: String?
let altKeyHash: String?
let title: String?
let meta: MetaBulletin?
let created: Int?
let updated: Int?
let deletedAt: Int?
let folderID: Int?
let hidden: Bool?
let cloudKeyHash: String?
let archived: Bool?
let altURL: String?
let url: String?

enum CodingKeys: String, CodingKey {
case id = "id"
case portalID = "portal_id"
case name = "name"
case size = "size"
case height = "height"
case width = "width"
case encoding = "encoding"
case type = "type"
case objectExtension = "extension"
case cloudKey = "cloud_key"
case s3URL = "s3_url"
case friendlyURL = "friendly_url"
case altKey = "alt_key"
case altKeyHash = "alt_key_hash"
case title = "title"
case meta = "meta"
case created = "created"
case updated = "updated"
case deletedAt = "deleted_at"
case folderID = "folder_id"
case hidden = "hidden"
case cloudKeyHash = "cloud_key_hash"
case archived = "archived"
case altURL = "alt_url"
case url = "url"
}
}

表格

var bulletins: BulletinHelper?

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "bullentinCell", for: indexPath)
let files = bulletins?.objects![indexPath.row]
cell.textLabel?.text = files?.name
cell.detailTextLabel?.text = "Weekly Bulletin"

return cell
}

我的排序尝试

它确实为我提供了排序的 ID,但我需要相关数据与其一起排序。

    let mysort: [Int] = [(bulletins?.objects![indexPath.row].id)!]
let sortedlist = mysort.sorted()
print(sortedlist)

最佳答案

根据@Kamran 的回复,我想出了这个按预期工作的解决方案。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "bullentinCell", for: indexPath)
let sortedlist = bulletins?.objects?.sorted(by: { ($0.id ?? 0) > ($1.id ?? 0) })
let files = sortedlist![indexPath.row]
cell.textLabel?.text = files.name
cell.detailTextLabel?.text = "Weekly Bulletin"

return cell
}

关于swift - 需要按 ObjectBulletin.id 对 tableview 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49790806/

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