如何用数据填充表格视图单元格?

时间:2018-09-24 17:49:17

标签: swift database firebase

我无法将个人资料图片和用户名添加到表格视图单元格中。这是我的Firebase数据库代码和我的Swift代码。当我填充表格视图时,我只会看到新消息和确定消息。如果您需要我班上的其他代码,请告诉我。

 chats

 -LN7VAbcvxP7R9ZWeuEN " ln1UW77qrkRBbZxTXlTEwwp7O4H2"

 featureImageUId: "ln1UW77qrkRBbZxTXlTEwwp7O4H2"

 lastMessage:  "Ok"

 lastUpdate: 1537740950.273408

 messageIds

 title: "New Message"

 uid: "-LN7VAbcvxP7R9ZWeuEN"

 users  

代码:

import UIKit
import Firebase

class InboxTableViewController: UITableViewController
{
    var currentUser: User!
    var chats = [Chat]()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.estimatedRowHeight = 66
        tableView.rowHeight = UITableViewAutomaticDimension
    }

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

        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let tabBarController = appDelegate.window!.rootViewController as! UITabBarController
        let firstNavVC = tabBarController.viewControllers!.first as! UINavigationController
        let newsfeedTVC = firstNavVC.topViewController as! NewsfeedTableViewController
        currentUser = newsfeedTVC.currentUser

        self.observeChats()
    }

    func observeChats() {
        let userChatIdsRef = WADatabaseReference.users(uid: currentUser.uid).reference().child("chatIds")

        userChatIdsRef.observe(.childAdded) { (snapshot: DataSnapshot) in
            let chatId = snapshot.key

            // go download that chat
            WADatabaseReference.chats.reference().child(chatId).observeSingleEvent(of: .value, with: { (snapshot: DataSnapshot) in
                let chat = Chat(dictionary: snapshot.value as! [String : Any])
                if !self.alreadyAdded(chat) {
                    self.chats.append(chat)
                    let indexPath = IndexPath(row: self.chats.count - 1, section: 0)
                    self.tableView.insertRows(at: [indexPath], with: .automatic)


                }
            })
        }
    }

    func alreadyAdded(_ chat: Chat) -> Bool {
        for c in chats {
            if c.uid == chat.uid {
                return true
            }
        }

        return false
    }

    func userSignedOut() {
        self.currentUser = nil
        self.chats.removeAll()
        self.tableView.reloadData()
    }

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

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ChatCell", for: indexPath) as! ChatTableViewCell
        let chat = chats[(indexPath as NSIndexPath).row]

        cell.chat = chat

        return cell
    }

    struct Storyboard {
        static let showContactsPicker = "ShowContactsPicker"
        static let showChatViewController = "ShowChatViewController"
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == Storyboard.showContactsPicker {
            let contactsPickerVC = segue.destination as! ContactsPickerViewController
            print(self.chats)
            contactsPickerVC.chats = self.chats
        } else if segue.identifier == Storyboard.showChatViewController {
            let chatVC = segue.destination as! ChatViewController
            let chatCell = sender as! ChatTableViewCell
            let chat = chatCell.chat
            chatVC.senderId = currentUser.uid
            chatVC.senderDisplayName = currentUser.fullName
            chatVC.chat = chat
            chatVC.currentUser = self.currentUser
            chatVC.hidesBottomBarWhenPushed = true
        }
    }
}

0 个答案:

没有答案