我需要通过ObjectBulletin中的id对tableview数据进行排序。我无法弄清楚如何对id应用排序并正确反映数据。
我的结构
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)
答案 0 :(得分:1)
根据@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
}
答案 1 :(得分:0)
struct BulletinHelper {
var objects: [ObjectBulletin]?
}
struct ObjectBulletin {
var name: String?
var id: Int?
}
var objects = [ObjectBulletin(name: "name 10", id: 10),
ObjectBulletin(name: "name 13", id: 13),
ObjectBulletin(name: "name 21", id: 21),
ObjectBulletin(name: "name 5", id: nil),
ObjectBulletin(name: "name 7", id: 7),
ObjectBulletin(name: "name 4", id: 4),
ObjectBulletin(name: "name 121", id: 121),
ObjectBulletin(name: "name 2", id: 2),
ObjectBulletin(name: "name 1", id: 1)]
var bulletins: BulletinHelper? = BulletinHelper(objects: objects)
let sortedList = bulletins?.objects?.sorted(by: { $0.id ?? 0 < $1.id ?? 0 })
bulletins?.objects = sortedList
bulletins?.objects?.map { print($0) }
这应该对你的数组进行排序。