提问者:小点点

如何按日期字符串对TableViewCells进行排序


如下所示,我希望按日期对TableViewCells进行排序。 为此,我有time,也称为timestampname

就在我重新加载数据之前,我试图对其进行排序,但不知为何,这没有任何效果。 它还提醒我不要使用排序的结果。 我明白这一点,但我不知道怎么解决。

import UIKit
import Firebase

class popularViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{

    @IBOutlet var table: UITableView!

//    var models = [PhotoPost]()
    var texttt = [TextPost]()

    override func viewDidLoad() {
        super.viewDidLoad()
        gettingPosts()
        table.register(popularTableViewCell.nib(), forCellReuseIdentifier: popularTableViewCell.identifier)
        table.register(featuredTableViewCell.nib(), forCellReuseIdentifier: featuredTableViewCell.identifier)
        table.register(textTableViewCell.nib(), forCellReuseIdentifier: textTableViewCell.identifier)

        table.delegate = self
        table.dataSource = self
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(true)
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return texttt.count
    }

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

        let cell = tableView.dequeueReusableCell(withIdentifier: textTableViewCell.identifier, for: indexPath) as! textTableViewCell
        cell.configure(with: self.texttt[indexPath.row])
        return cell

    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        return 300
    }

    func gettingPosts(){

        let db = Firestore.firestore()
        let postsRef = db.collection("posts")
        postsRef.addSnapshotListener { (querySnapshot, error) in
            guard let snapshot = querySnapshot else {
            print("Error fetching snapshots: \(error!)")
            return
            }
            snapshot.documentChanges.forEach { diff in
                if (diff.type == .added){
                    let data = diff.document.data()
                    let Name = data["username"] as! String
                    let text = data["description"] as! String
                    let likes = data["likes"] as! Int
                    let typ = data["postType"] as! Int
                    let pfp = data["profileImage"] as! String
                    let uid = data["uid"] as! String
                    let pic = data["picture"]
                    let time = data["time"] as! String

                    if typ == 0{                                                // Text post
                        let dasDing = TextPost(numberOfComments: 0, username: Name, timestampName: time, userImageName: pfp, textName: text)
                        self.texttt.append(dasDing)
                        self.texttt.sorted(by: { $0.timestampName < $1.timestampName }) //WARNING: Result of call to 'sorted(by:)' is unused
                        self.table.reloadData()
                    }

struct TextPost {
    let numberOfComments: Int
    let username: String
    let timestampName: String
    let userImageName: String
    let textName: String
}


共1个答案

匿名用户

使用sort而不是sorted。 sorted方法返回一个新的排序数组,另一方面,sort方法对调用它的数组进行排序。

self.texttt.sort(by: { $0.timestampName < $1.timestampName })

使用sorted:

self.texttt = self.texttt.sorted(by: { $0.timestampName < $1.timestampName })