How make your tableView show new or update element at the top of your tableView PT.2

早过忘川 提交于 2019-12-12 05:00:10

问题


I can't seem to get new messages sent from new user or new message sent from users already in tableview to go to the like with most messaging apps. I've ask this question before and tried to follow the directions in a link that someone kind enough to send me (How do you properly order data from Firebase chronologically) but I still can't get the desired effect no matter matter I try

func getChatsFeed(){
//following the links instruction "timeStamp" is suppose help return fireBase children in reverse order. "atValue: time" is a value for the time when my viewDidload.
    let queryChats = Database.database().reference().queryOrdered(byChild: "timeStamp").queryEnding(atValue: time, childKey: "timeStamp")

    queryChats.observe(.childAdded) { (snapshot) in

        if let snapvalue = snapshot.value as? Dictionary<String,Any>, let
            sender = snapvalue["sender"] as? String, let receiver =
            snapvalue["receiver"] as? String, let message = snapvalue["messageBody"] as?
            String, let key = snapvalue["key"] as? String, let timeStamp = snapvalue["timeStamp"] as? String{

            let newMessage = FeedMessage(sender: sender, receiver: receiver, messageBody: message, key: key, timeStamp: timeStamp)

            feedUpdate.append(newMessage)
            self.chatsTableView.reloadData()

        }


    }
}

The link I included received a respectable amount of praise for being solution a to a lot of StackOverflow users problem so maybe I messed up somewhere, being not fully in the know of all the different Firebase methods, this could be the case. Any pointers anyone?


回答1:


With the other approaches, you've used where data is loaded into feedUpdate, simply do this:

    chatsTableView.reloadData()
    let indexPath:IndexPath = IndexPath(row:0, section:0)
    chatsTableView.scrollToRow(at: indexPath, at: UITableViewScrollPosition .top, animated: true)


来源:https://stackoverflow.com/questions/46991529/how-make-your-tableview-show-new-or-update-element-at-the-top-of-your-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!