gpt4 book ai didi

ios - 使用 Swift 按字母顺序对 TableView 单元格进行排序

转载 作者:搜寻专家 更新时间:2023-11-01 06:15:32 25 4
gpt4 key购买 nike

我正在使用 firebase 将数据加载到 tableview 单元格中,这是我的数据结构。

struct postStruct {
let title : String!
let author : String!
let bookRefCode : String!
let imageDownloadString : String!
let status : String!
let reserved : String!
let category : String!
let dueDate : String!
}

现在我需要按字母顺序对保存数据的 tableview 单元格进行排序,标题以 A 开头在顶部,Z 在底部。

class DirectoryTableView: UITableViewController {

var posts = [postStruct]()

override func viewDidLoad() {

let databaseRef = Database.database().reference()

databaseRef.child("Books").queryOrderedByKey().observe(.childAdded, with: {
snapshot in

var snapshotValue = snapshot.value as? NSDictionary
let title = snapshotValue!["title"] as? String
snapshotValue = snapshot.value as? NSDictionary

let author = snapshotValue!["author"] as? String
snapshotValue = snapshot.value as? NSDictionary

let bookRefCode = snapshotValue!["bookRefCode"] as? String
snapshotValue = snapshot.value as? NSDictionary

let status = snapshotValue!["status"] as? String
snapshotValue = snapshot.value as? NSDictionary

let reserved = snapshotValue!["reserved"] as? String
snapshotValue = snapshot.value as? NSDictionary

let category = snapshotValue!["category"] as? String
snapshotValue = snapshot.value as? NSDictionary

let dueDate = snapshotValue!["dueDate"] as? String
snapshotValue = snapshot.value as? NSDictionary


self.posts.insert(postStruct(title: title, author: author, bookRefCode: bookRefCode, status: status, reserved: reserved, category: category, dueDate: dueDate) , at: 0)
self.tableView.reloadData()


})



override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return posts.count

}




override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

var cell = tableView.dequeueReusableCell(withIdentifier: "cell")

let databaseRef = Database.database().reference()

let label1 = cell?.viewWithTag(1) as! UILabel
label1.text = posts[indexPath.row].title

let label2 = cell?.viewWithTag(2) as! UILabel
label2.text = posts[indexPath.row].author

let label3 = cell?.viewWithTag(3) as! UILabel
label3.text = posts[indexPath.row].bookRefCode

let label4 = cell?.viewWithTag(4) as! UILabel
label4.text = posts[indexPath.row].status

let label5 = cell?.viewWithTag(5) as! UILabel
label5.text = posts[indexPath.row].category

let image1 = cell?.viewWithTag(6) as! UILabel
image1.text = posts[indexPath.row].imageDownloadString

let label6 = cell?.viewWithTag(7) as! UILabel
label6.text = posts[indexPath.row].reserved

let label9 = cell?.viewWithTag(9) as! UILabel
label9.text = posts[indexPath.row].dueDate

return cell!

}

任何想法,请帮助!我尝试用不同的方法对它们进行排序,但我很困惑!

最佳答案

正如我在评论中所说,您不需要对单元格进行排序,您需要对数据源数组进行排序,为此添加此行 self.posts.sort(by: {$0.title < $1.title}) 在你的下面 self.posts.insert(postStruct(title: title, author: author, bookRefCode: bookRefCode, status: status, reserved: reserved, category: category, dueDate: dueDate) , at: 0)添加这一行你的帖子数组将保持有序,每次你添加另一个帖子时都会再次排序

你的viewDidLoad完整代码与排序

override func viewDidLoad() {

let databaseRef = Database.database().reference()

databaseRef.child("Books").queryOrderedByKey().observe(.childAdded, with: {
snapshot in

var snapshotValue = snapshot.value as? NSDictionary
let title = snapshotValue!["title"] as? String
snapshotValue = snapshot.value as? NSDictionary

let author = snapshotValue!["author"] as? String
snapshotValue = snapshot.value as? NSDictionary

let bookRefCode = snapshotValue!["bookRefCode"] as? String
snapshotValue = snapshot.value as? NSDictionary

let status = snapshotValue!["status"] as? String
snapshotValue = snapshot.value as? NSDictionary

let reserved = snapshotValue!["reserved"] as? String
snapshotValue = snapshot.value as? NSDictionary

let category = snapshotValue!["category"] as? String
snapshotValue = snapshot.value as? NSDictionary

let dueDate = snapshotValue!["dueDate"] as? String
snapshotValue = snapshot.value as? NSDictionary


self.posts.insert(postStruct(title: title, author: author, bookRefCode: bookRefCode, status: status, reserved: reserved, category: category, dueDate: dueDate) , at: 0)
self.posts.sort(by: {$0.title < $1.title})
self.tableView.reloadData()


})

关于ios - 使用 Swift 按字母顺序对 TableView 单元格进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45950294/

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