gpt4 book ai didi

ios - 根据内容自动更改单元格高度 - Swift

转载 作者:搜寻专家 更新时间:2023-10-31 08:12:00 25 4
gpt4 key购买 nike

在 Swift 中使用 UITableView,有人可以帮我根据标签、图片和描述自动更改单元格的高度吗?

所有信息都正确传递,我只需要帮助格式化它。

我尝试使用 cell.frame.size.height 调整它,但没有效果。我可以更改 Storyboard中单元格的大小,但如果可能的话,我希望它更加动态。

提前致谢!

我的cell如下:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell

// Creator
let creator = self.photos[indexPath.row].creator
let user = UILabel()
user.frame = CGRectMake(self.headerView.frame.origin.x, self.headerView.frame.origin.y + self.headerView.frame.height + 3, self.view.frame.width, 12)
user.text = creator
cell.addSubview(user)

// Photo
let picture = self.photos[indexPath.row].image()
let imageView = UIImageView(image: picture!)
imageView.frame = CGRectMake(user.frame.origin.x, user.frame.origin.y + user.frame.height, self.view.frame.width, 400)
imageView.contentMode = UIViewContentMode.ScaleAspectFit
cell.addSubview(imageView)

// Photo description
let description = UITextView()
let des = self.photos[indexPath.row].photoDescription
description.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y + imageView.frame.height, self.view.frame.width, 65)
if des != nil {
description.text = des!
description.font = UIFont.systemFontOfSize(16)
description.editable = false
description.scrollEnabled = false
}
cell.addSubview(description)

cell.frame.size.height = user.frame.size.height + imageView.frame.size.height + description.frame.size.height + 30

return cell
}

最佳答案

为了使单元格自动调整大小,您需要做两件事:

  1. 使用自动版式排列单元格内的元素。底部空间和顶部空间限制将根据需要插入单元格大小。
  2. viewDidLoad 中设置 tableView.rowHeight = UITableViewAutomaticDimension

关于ios - 根据内容自动更改单元格高度 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31236546/

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