gpt4 book ai didi

Swift 自动尺寸单元格高度不起作用

转载 作者:行者123 更新时间:2023-11-30 11:01:04 24 4
gpt4 key购买 nike

用于消息 ViewControllerUITableView 单元格仅在我向上或向下滚动表格 View 时以及有时在发送新消息时才能获得正确的高度尺寸。我见过大多数人用 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: 1*1024*1024, 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)
}

}

}
}

约束,后代是针对标签本身的,另一个是背景: enter image description here

这是当我输入长文本时的结果,它被剪切而不是根据内容调整高度: enter image description here

当我再次输入完全相同的文本并单击“发送”时,我仅获得最新文本的所需结果: enter image description here

最佳答案

为什么要两者都放

messageTableView.estimatedRowHeight = 120.0
返回UITableView.automaticDimension

在你的 tableView(_ tableView:,estimatedHeightForRowAt indexPath:) 方法中?这样做没有意义,使用estimatedHeight 来帮助tableView 创建单元格。因此,只需返回 CGFloat 即可完成工作,即

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 120

}

为您的单元格添加AutomaticDimension。添加此:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}

关于Swift 自动尺寸单元格高度不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53362061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com