UITableViewCell没有显示UILabel

时间:2018-03-19 20:59:00

标签: ios swift uitableview tableview

我正在尝试在我的应用程序中实现聊天大厅。我有一个UITableViewCell,它以编程方式添加了一个标签。一旦开火,没有任何表现。这是我的代码ViewController包含UITableView

import UIKit
import Firebase

class MessagesViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
{

    private var ContactNames = [String]()
    private var ContactPicturesURLs = [String]()

    @IBOutlet weak var ChatsTableView: UITableView!
    override func viewDidLoad()
    {
        super.viewDidLoad()

        ChatsTableView.dataSource = self
        ChatsTableView.delegate = self
        self.view.layer.backgroundColor = UIColor.white.cgColor
        populateActiveChats()
    }

    private func populateActiveChats()
    {
        let loggedOnUserID = Auth.auth().currentUser?.uid
        let ref = Constants.refs.databaseChatsLite.child(loggedOnUserID!)

            // Retrieve the products and listen for changes
        ref.observe(.value, with:
                { (snapshot) in

                    for child in snapshot.children.allObjects as! [DataSnapshot]
                    {
                        // Code to execute when new product is added
                        let chatValueDictionary = child.value as? NSDictionary

                        self.AddChatToCollections(chatAsDictionary: chatValueDictionary)
                        self.DispatchQueueFunc()

                    }
            })
    }

    func AddChatToCollections(chatAsDictionary: NSDictionary!)
    {
        let contactName = chatAsDictionary["userName"] as! String        
        self.ContactNames.append(contactName)
    }

    func DispatchQueueFunc()
    {
        DispatchQueue.main.async
            {
                self.ChatsTableView.reloadData()
        }
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = ChatsTableView.dequeueReusableCell(withIdentifier: "chat_room_cell", for: indexPath) as! PrivateChatUITableViewCell

        var index = indexPath.row
        cell.ContactName.text = ContactNames[index]

        return cell

    }

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

        chatIndexToLoad = indexPath.row
        performSegue(withIdentifier: "segue_to_chatroom", sender: self)
    }

    var chatIndexToLoad: Int = -1

    override func prepare(for segue: UIStoryboardSegue, sender: Any?)
    {
        if (segue.identifier == "segue_to_chatroom")
        {
            let destination = segue.destination as! PrivateChatViewController
            destination.senderId = Constants.refs.currentUserInformation?.uid
            destination.senderDisplayName = Constants.refs.currentUserInformation?.displayName

        }
    }

}

这是我TableViewCell的代码:

import UIKit

class PrivateChatUITableViewCell: UITableViewCell
{
    var ContactName: UILabel!


    required init?(coder aDecoder: NSCoder)
    {
        super.init(coder: aDecoder)

        commonInit()
    }

    func commonInit()
    {
        ContactName = UILabel()

        self.addSubview(ContactName)
    }

    override func layoutSubviews()
    {
        let margins = self.contentView.layoutMarginsGuide

        ContactName.center = self.center

    }
}

然而,我的手机显示空白,如果它确实显示:

TableView

我调试了我的代码,并确保单元格的文本显示在其标签中,因此它不是空的,如下图所示:

Cell not empty

有没有人有标签的解决方案没有出现?

另外,我调试了commonInit中的PrivateChatUITableViewCell函数,它确实点击了addSubView和约束命令

1 个答案:

答案 0 :(得分:0)

可能是因为您在表格单元格中进行了初始化。看起来你在表格中有一个框架零标签。您应该使用公共init中设置的NSLayoutConstraint设置标签的位置,或者您可以使用上面的方法。

lazy var ContactName : UILabel = {
    let view = UILabel()
    view.textColor = UIColor.white
    view.textAlignment = .center
    return view
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    self.addSubview(ContactName)
    //set your constraints by NSLayoutConstraint
}
相关问题