Как мне заполнить ячейку табличного представления данными? - PullRequest
0 голосов
/ 24 сентября 2018

Я не могу добавить свое изображение профиля и имя пользователя в свою ячейку табличного представления.Вот мой код базы данных 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
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...