Ios 使用Swift按字母顺序对TableView单元格排序

Ios 使用Swift按字母顺序对TableView单元格排序,ios,swift,uitableview,sorting,firebase-realtime-database,Ios,Swift,Uitableview,Sorting,Firebase Realtime Database,我正在使用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 : Strin

我正在使用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.insert(postStruct)下面添加此行
self.posts.sort(by:{$0.title
(标题:title,作者:author,bookRefCode:bookRefCode,状态:status,reserved:reserved,category:category,dueDate:dueDate),地址:0)
添加此行后,您的帖子数组将保持有序,并且每次添加另一篇帖子时都将再次排序

您的视图加载带有排序的完整代码

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()


})
override func viewDidLoad(){
让databaseRef=Database.Database().reference()
databaseRef.child(“Books”).queryOrderedByKey().observe(.childAdded,带有:{
快照
var snapshotValue=snapshot.value作为NSDictionary
让title=snapshotValue![“title”]作为?字符串
snapshotValue=snapshot.value作为NSDictionary
让author=snapshotValue![“author”]作为?字符串
snapshotValue=snapshot.value作为NSDictionary
让bookRefCode=snapshotValue![“bookRefCode”]作为?字符串
snapshotValue=snapshot.value作为NSDictionary
让status=snapshotValue![“status”]作为?字符串
snapshotValue=snapshot.value作为NSDictionary
让reserved=snapshotValue![“reserved”]作为?字符串
snapshotValue=snapshot.value作为NSDictionary
让category=snapshotValue![“category”]作为?字符串
snapshotValue=snapshot.value作为NSDictionary
让dueDate=snapshotValue![“dueDate”]作为?字符串
snapshotValue=snapshot.value作为NSDictionary
self.posts.insert(postStruct(标题:标题,作者:作者,bookRefCode:bookRefCode,状态:状态,保留:保留,类别:类别,dueDate:dueDate),地址:0)
self.posts.sort(按:{$0.title<$1.title})
self.tableView.reloadData()
})

正如我在评论中所说,您不需要对单元格进行排序,而需要对数据源数组进行排序,要做到这一点,请在您的
self.posts.insert(postStruct)下面添加此行
self.posts.sort(by:{$0.title<$1.title})
(标题:title,作者:author,bookRefCode:bookRefCode,状态:status,reserved:reserved,category:category,dueDate:dueDate),地址:0)
添加此行后,您的帖子数组将保持有序,并且每次添加另一篇帖子时都将再次排序

您的视图加载带有排序的完整代码

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()


})
override func viewDidLoad(){
让databaseRef=Database.Database().reference()
databaseRef.child(“Books”).queryOrderedByKey().observe(.childAdded,带有:{
快照
var snapshotValue=snapshot.value作为NSDictionary
让title=snapshotValue![“title”]作为?字符串
snapshotValue=snapshot.value作为NSDictionary
让author=snapshotValue![“author”]作为?字符串
snapshotValue=snapshot.value作为NSDictionary
让bookRefCode=snapshotValue![“bookRefCode”]作为?字符串
snapshotValue=snapshot.value作为NSDictionary
让status=snapshotValue![“status”]作为?字符串
snapshotValue=snapshot.value作为NSDictionary
让reserved=snapshotValue![“reserved”]作为?字符串
snapshotValue=snapshot.value作为NSDictionary
让category=snapshotValue![“category”]作为?字符串
snapshotValue=snapshot.value作为NSDictionary
让dueDate=snapshotValue![“dueDate”]作为?字符串
snapshotValue=snapshot.value作为NSDictionary
self.posts.insert(postStruct(标题:标题,作者:作者,bookRefCode:bookRefCode,状态:状态,保留:保留,类别:类别,dueDate:dueDate),地址:0)
self.posts.sort(按:{$0.title<$1.title})
self.tableView.reloadData()
})

您不需要对单元格进行排序,而需要对数据源数组进行排序我该如何对数据源数组进行排序?您不需要对单元格进行排序,而需要对数据源数组进行排序我该如何对数据源数组进行排序?您知道如何将所有以A开头的单元格都放在一个部分中,然后将它们自己和B放在一个部分中吗,还有Cs,等等?我会检查一下,但是你可以发布一个关于这个@joeyhal的问题,你知道如何把所有以A开头的,然后把它们自己放在一个部分,还有Bs,Cs,等等吗?我会检查一下,但是你可以发布一个关于这个@joeyhal的问题