gpt4 book ai didi

ios - UITableViewCell 高度不正确,直到通过滚动重新创建单元格

转载 作者:行者123 更新时间:2023-11-28 12:58:43 25 4
gpt4 key购买 nike

我已经尝试在我修改单元格内容的每个地方设置 cell.setNeedsLayout() 并调用 cell.layoutIfNeeded() ,但无论在我向下/向上滚动之前,单元格高度不正确。

不正确(初始加载):

正确(滚动后):

cellForRowAtIndexPath:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("OMCFeedCell", forIndexPath: indexPath) as! OMCFeedTableViewCell
let p = posts[indexPath.row]

cell.profileImage.layer.cornerRadius = cell.profileImage.frame.size.width / 2;
cell.profileImage.layer.masksToBounds = true;
cell.profileImage.contentMode = .ScaleAspectFit
cell.profileImage.image = UIImage(named: "profile_blank")

let API = userAPI()
API.getByID(p.postEntity) {
(result: User?) in

if let u = result {
cell.profileName.text = u.name

let uploads = uploadAPI()
uploads.tryGetImage(u.image) {
(result: UIImage?) in

if let i = result {
cell.profileImage.image = i
}
cell.setNeedsLayout()
cell.layoutIfNeeded()
}
}
}

cell.selectionStyle = .None
let data = p.content!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let content = try! JSON(NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers))

if let image = content["image"].string {
let uploads = uploadAPI()
uploads.tryGetImage(image) {
(result: UIImage?) in

if let i = result {
let oFrame = cell.postImage.frame
let width = oFrame.width
let height = (i.size.height / i.size.width) * width
cell.postImageHeight.constant = height
cell.postImageHeight.priority = 999
cell.postImage.contentMode = .ScaleAspectFit
cell.postImage.image = i
} else {
cell.postImageHeight.priority = 1
cell.postImage.image = nil
}
cell.setNeedsLayout()
cell.layoutIfNeeded()
}
} else {
cell.postImage.image = nil
cell.postImageHeight.constant = 0
}

cell.postText.text = ""
if let text = content["text"].string {
cell.postText.text = text
}

cell.postTimestamp.text = JSQMessagesTimestampFormatter.sharedFormatter().attributedTimestampForDate(p.timestamp).string
cell.setNeedsLayout()
cell.layoutIfNeeded()

return cell
}

最佳答案

如果您在表格单元格显示后更新其内容(因此它的高度),表格 View 将不会注意到它。你需要告诉它。

您可以尝试 reloadRowsAtIndexPaths:withRowAnimation:,不确定是否足够。

关于ios - UITableViewCell 高度不正确,直到通过滚动重新创建单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34373731/

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