将Tableview单元格按字母顺序排列 - Swift 3和Firebase数据库
问题描述:
我正在使用firebase将数据加载到tableview单元格中,这里是我的数据结构。将Tableview单元格按字母顺序排列 - Swift 3和Firebase数据库
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!
}
现在我有排序的职位按字母顺序使用
self.posts.sort(by: { $0.title < $1.title })
,但我不知道如何把以A的部分“A”开头的细胞,“B”,等等上。
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!
}
任何想法,请帮助!我试图用不同的方法对它们进行排序,但我很困惑!
答
首先,你需要一个新的结构,以节省您的帖子将它划分的首个冠军头衔的信:let postsAlphabetically: [String:[Post]]
没有指定雨燕版本,所以假设你会迁移到斯威夫特4,你可以使用单个行中的数据和组排序是:
let postsAlphabetically = Dictionary(grouping: self.posts) { $0.title.first! }
// E.g: ["A" : ["A book", "Another book"], "B" : ["Blue Book", "Black Book"]]
后来,你会在你的cellForRowAt
方法使用postsAlphabetically
代替self.posts
。
P.S:Swift中的类型名称是用上部骆驼案例编写的(PostSimple
而不是postSimple
)。类型本身被省略(Author
而不是AuthorClass
)。