【问题标题】:UITableViewCell not showing UILabelUITableViewCell 不显示 UILabel
【发布时间】:2018-03-19 20:59:00
【问题描述】:

我正在尝试在我的应用程序中实现聊天大厅。我有一个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

    }
}

但我的单元格显示为空白,如果它确实显示的话:

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

有没有人解决标签不出现的问题?

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

【问题讨论】:

    标签: ios swift uitableview tableview


    【解决方案1】:

    这可能是因为您在表格单元格中进行了初始化。似乎您在表格中有一个零帧标签。设置标签的位置应该使用普通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
    }
    

    【讨论】:

    • 我收到一条错误消息,提示我必须实现“必需的初始化?”功能。
    • 另外,它会激活“需要初始化?”功能,所以我必须实现它。我应该将您的 init 函数的主体复制到它吗?
    • 您不必在所需的初始化代码中添加任何内容。
    • 我添加了默认的“需要初始化?”代码并且它确实激活了它,并且默认为“抛出异常”。除了删除它我还需要做什么吗?
    • 我尝试添加约束,但它不起作用。我不确定下一步该做什么
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    相关资源
    最近更新 更多