Мои UITableView
ячейки для сообщения ViewController
получают правильное измерение высоты только при прокрутке таблицы вверх или вниз, а иногда и при отправке нового сообщения.Я видел, как большинство людей решают это с помощью cell.layoutIfNeeded
, но для меня это не работает.Есть идеи почему?
automaticDimension и cell.layoutIfNeeded:
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
messageTableView.estimatedRowHeight = 120.0
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customMessageCell", for: indexPath) as! CustomMessageCell
cell.ourMessageBackground.backgroundColor = UIColor(displayP3Red: 59/255.0, green: 89/255.0, blue: 152/255.0, alpha: 1)
cell.ourMessageBody.textColor = UIColor.white
cell.avatarImageView.backgroundColor = UIColor(white: 0.95, alpha: 1)
cell.messageBackground.backgroundColor = UIColor(white: 0.95, alpha: 1)
cell.layoutIfNeeded() //<-- Layout if needed
return cell
}
Код для настройки ячейки:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let cell = cell as? CustomMessageCell else { return }
//Sets cell to message
let senderId = messageArray[indexPath.row].fromId
if senderId == Auth.auth().currentUser?.uid as String? {
//Messages We Sent
cell.ourMessageBody.text = messageArray[indexPath.row].messageBody
cell.avatarImageView.isHidden = true
cell.messageBackground.isHidden = true
cell.ourMessageBackground.isHidden = false
} else {
//Messages someone else sent
cell.messageBody.text = messageArray[indexPath.row].messageBody
cell.avatarImageView.isHidden = false
cell.messageBackground.isHidden = false
cell.ourMessageBackground.isHidden = true
cell.layoutIfNeeded()
//toId ProfileImage
if let imageID = toId {
let imagesStorageRef = Storage.storage().reference().child("profilepic/").child(imageID)
imagesStorageRef.getData(maxSize: 11024, completion: { (data, error) in
if error != nil{
print(error)
return
}
DispatchQueue.main.async {
guard let c = tableView.cellForRow(at: indexPath) else { return }
cell.avatarImageView?.image = UIImage(data: data!)
}
})
}
}
}
Код для получения сообщений из Firebase:
func retrieveMessages() {
SwipingView.myAdvertiserVar()
let testUserOrAd = SwipingView.myAdvertiserVar.advertiser
if testUserOrAd == true{
if let testId = self.toId{
let MessageDB = Database.database().reference().child("Users").child(toId!).child("Messages").child(uid!)
MessageDB.observe(.childAdded) { (snapshot) in
let snapshotValue = snapshot.value as? [String: AnyObject]
let text = snapshotValue!["MessageBody"] as? String
let fromId = snapshotValue!["FromId"] as? String
let time = snapshotValue!["TimeStamp"] as? Int
let message = Message()
message.messageBody = text
message.fromId = fromId
self.user.timeStamp = time
self.messageArray.append(message)
self.messageTableView.reloadData()
self.messageTableView.scrollToRow(at: IndexPath(row: self.messageArray.count-1, section: 0), at: .bottom, animated: false)
}
}
}
}
Ограничения: потомок для самой метки, другой - фон:
Это результат, когда я набираю длинный текст, онобрезается вместо высоты, настроенной на содержание:
*1024* Когда я снова набираю тот же самый точный текст и нажимаю кнопку отправить, я получаю желаемый результат только для самого последнего текста: