【问题标题】:Change the height of a TableViewCell depending on if it has an image改变 TableViewCell 的高度取决于它是否有图像
【发布时间】:2021-10-22 05:30:05
【问题描述】:

我有一个表格视图,其中有表格视图单元格,其中填充了用户在应用程序上发布的帖子。用户可以发布仅包含文字的帖子,也可以在文字中包含图片。

我遇到的问题是根据帖子是否包含图像来设置单元格高度。例如,如果帖子包含一张图片,我希望单元格高度 = 400,但如果它只包含文本,那么我希望单元格高度 = 150。

目前我使用此代码将高度固定为 400,但我不知道如何进行上述修改。

 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    //auto cell height
    return 400
    
}

后模型看起来像这样...

class Posts {
var id: String
var author: String
var text: String
var createdAt: Date
var degree: String
var university: String
var uid: String
var photoURL: String
var url: URL
var postID: String
var likes: Int

init(id:String, author:String, text:String, timestamp:Double, degree:String, university:String, uid:String, photoURL:String, url:URL, postID:String, likes:Int) {
    self.id = id
    self.author = author
    self.text = text
    self.createdAt = Date(timeIntervalSince1970: timestamp / 1000)
    self.degree = degree 
    self.university = university
    self.uid = uid
    self.photoURL = photoURL
    self.url = url
    self.postID = postID
    self.likes = likes
    
   }
}

【问题讨论】:

  • 您使用的是自定义 TableViewCell 吗?因为您总是可以设计一个带有图像的自定义单元格(并相应地调整约束),如果没有图像可以放入其中,只需隐藏 imageView。
  • 好吧,这听起来是个好主意。我将为 tableView heightForRowAt 函数设置什么?这样它在有图像和没有图像时都可以工作
  • 您实际上根本不需要设置该功能。您需要注意一些自动布局的事情(请参阅下面的答案,因为我无法将其全部放在此评论中),但是如果您做得对,则单元格应该能够在没有该功能的情况下自行调整大小。

标签: swift xcode uitableview tableview


【解决方案1】:

一个相当简单的方法是创建一个自定义视图单元格并在需要时隐藏 imageView。

创建自定义 tableView 单元格并确保添加一个 .xib 文件以便能够直观地编辑所有内容:

将行高设置为自动,以便 tableView 知道将高度设置为您需要的任何值。

然后,您可以将 text 和 imageView 添加到此自定义单元格中。我之前的工作方式是将两个元素都添加到 stackView 中并设置约束。

确保您已为 stackView 内部的 label 和 imageView 的高度以及从 stackView 到 superView 的底部和顶部的距离设置了约束。这让自动布局知道单元格的高度

现在,无论何时隐藏 imageView(当您没有要显示的图像时),单元格都应自动调整为更小。

【讨论】:

    【解决方案2】:

    我最终将此代码添加到 tableview cellforrowat 索引路径函数中,以便正确渲染图像。

    let imageIndex = posts[indexPath.row].mediaURL
                let url = NSURL(string: imageIndex)! as URL
                if let imageData: NSData = NSData(contentsOf: url) {
                   let image = UIImage(data: imageData as Data)
                    let newWidth = cell.MediaPhoto.frame.width
                    let scale = newWidth/image!.size.width
                    let newHeight = image!.size.height * scale
                    UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight))
                    image!.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight))
                        let newImage = UIGraphicsGetImageFromCurrentImageContext()
                        UIGraphicsEndImageContext()
                        cell.MediaPhoto.image = newImage
                    cell.MediaPhoto.contentMode = .scaleToFill
                    cell.MediaPhoto.clipsToBounds = true
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-02
      • 2016-12-27
      相关资源
      最近更新 更多