0

I know that there is a thread dedicated for asking autosizing table view cell in here: why UITableViewAutomaticDimension not working?

but maybe may case little bit different.

so I want to make autosizing of my table view cell based on the text assigned of my label. I think I have tried to set UITableViewAutomaticDimension and also have tried to set the all constraints (top, bottom, right left) with respect to its superview/ cell container. and I have also set the number of line to be zero like this

here is the code of my VC:

class ChatLogVC: UIViewController {


    @IBOutlet weak var chatLogTableView: UITableView!

    var messageList = [ChatMessage]()

    override func viewDidLoad() {
        super.viewDidLoad()

        listenForMessages()

        chatLogTableView.estimatedRowHeight = UITableView.automaticDimension
        chatLogTableView.rowHeight = UITableView.automaticDimension


    }


    private func listenForMessages() {

        // Get data from firebase         

        let fromId = currentUser.uid
        let toId = otherUser.uid

        let ref = Database.database().reference(withPath: "/user-messages/\(fromId)/\(toId)")

        ref.observe(DataEventType.childAdded, with: { (snapshot) in

            let messageDictionary = snapshot.value as? [String : Any] ?? [:]
            let message = ChatMessage(dictionary: messageDictionary)
            self.messageList.append(message)
            self.chatLogTableView.reloadData()

        })
    }


}



   //MARK: - UI Table View Data Source & Delegate
extension ChatLogVC : UITableViewDataSource {

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cellFrom = tableView.dequeueReusableCell(withIdentifier: "fromChatViewCell", for: indexPath) as! fromChatViewCell

        let chatMessage = messageList[indexPath.row]

        cellFrom.chatMessageData = chatMessage
        cellFrom.otherUserData = otherUser
        return cellFrom

    }

and here is the table view cell:

class fromChatViewCell: UITableViewCell {

    @IBOutlet weak var fromChatLabel: UILabel!
    @IBOutlet weak var fromProfilePictureImageView: UIImageView!


    var chatMessageData : ChatMessage?
    var otherUserData : User?


    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        updateUI()
    }

    func updateUI() {

        guard let message = chatMessageData, let otherUser = otherUserData else {return}
        guard let url = URL(string: otherUser.profileImageURL) else {return}
        fromChatLabel.text = message.text
        fromProfilePictureImageView.kf.setImage(with: url, options: [.transition(.fade(0.2))])

    }

}

}

I have also tried to connect the datasource and delegate to the VC: enter image description here

and here is my label constraint: enter image description here

and I have also set the number of line to be zero: enter image description here

and here is my problem:

if in my storyboard I set the text of my label using just few word, then the autosizing doesn't work, it seems that it will only show one line of text even though actually the text from firebase is more than one line

enter image description here

result (app running):

enter image description here

but if I set the text on my label using a lot of words, then the autosizing table view cell will work and will show all the string from the firebase database:

enter image description here

it will expand, but the autosizing still doens't work since the label still clipping enter image description here

what went wrong in here? I am really confused

I have tried to change bottom label constraint priority to 200, sometimes the autosizing worked but the other time it will not. maybe my problem related to asynchrounous problem, I have tried to reload the table view rght after get the massge from firebase. but I don't know the root cause of my problem

Alexa289
  • 8,089
  • 10
  • 74
  • 178
  • give label height as >= 0 in Interface Design (add constraint). Use word wrap not truncate tail also – ares777 Mar 27 '19 at 10:10
  • From what you are showing it seems that both of the situations cut of some text; even the last image with long text has "..." at the end. Are you assigning the new text to label too late, asynchronously? If so, you will need to refresh the cell for cell to actually resize. I suspect your label is OK but the cell is not. You can try this by setting the bottom constrain priority to 200. If this makes label extend beyond the cell then that is the case. – Matic Oblak Mar 27 '19 at 10:16
  • @MaticOblak I have tried to change constraint priority to 200, sometimes it works but the other time it will not. maybe my problem related to asynchrounous problem, I have tried to reload the table view rght after get the massge from firebase – Alexa289 Mar 27 '19 at 10:50
  • @Alexa289 the "200 priority" was just a test, not a fix. Show us how and where you assign text to your label. – Matic Oblak Mar 27 '19 at 10:51
  • @MaticOblak I have edited my question above, I have added my cellForRowAt method (datasource) and also the table view cell, where I exactly assign the text to my label. – Alexa289 Mar 27 '19 at 10:58
  • @Alexa289 Is it possible you are only missing `cellFrom.updateUI()` before you return the cell in cell-for-row method? The text should be applied to label as soon as possible. – Matic Oblak Mar 27 '19 at 11:01

0 Answers0