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:
and here is my label constraint:
and I have also set the number of line to be zero:
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
result (app running):
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:
it will expand, but the autosizing still doens't work since the label still clipping
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