【问题标题】:iOS load image into table view celliOS将图像加载到表格视图单元格中
【发布时间】:2018-07-02 12:18:16
【问题描述】:

更新

现在图像在加载时显示,但仍有一个我无法解决的问题。 即使三个视图在垂直堆栈中,图像也会与单元格的内容重叠(第一个是图像,接下来的两个是标签视图)。 我想知道一个视图如何与堆栈视图中的其他视图重叠

现在我的 talbe 视图委托方法如下所示:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell: BaseTableViewCell!
    if let contents = contents {
        let content = contents[indexPath.row]
        if let imageUrl = content.imageUrl, let url = URL(string: imageUrl) {
            cell = tableView.dequeueReusableCell(withIdentifier: "ImageContentRow", for: indexPath) as! ContentWithImageTableViewCell
            Alamofire.request(url).responseImage(completionHandler: { (response) in
                if let image = response.result.value {
                    cell.imageView?.image = image
                    cell.setNeedsLayout()
                }
            })
        }else{
            cell = tableView.dequeueReusableCell(withIdentifier: "ContentRow", for: indexPath) as! ContentTableViewCell
        }
        cell.contentTitleVeiw.text = content.title
        cell.contentLeadView.text = content.lead
    }
    return cell!
}

我对 iOS 很陌生。我已经看过教程并观看了视频课程,现在我想实现我的第一个应用程序。 我尝试阅读提要并显示内容的标题、线索和图像(如果有的话)。 我定义了一个带有 UIMageVeiw 和两个 UILable 的单元格,最后我将它们嵌入到垂直 StackView 中(我将分布设置为填充)。 一切正常,但图像不会自动显示,只要我单击单元格或滚动 TableVeiw。 即使我将图像视图设置为 Aspect Fit(并将其嵌入到垂直堆栈视图的顶部),当图像显示时,它仍会保持其原始大小并与单元格内容重叠。 我试图找出我做错了两天,但我无法解决它。

我尝试像这样显示数据:

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell: BaseTableViewCell!
    if let contents = contents {
        let content = contents[indexPath.row]
        if let imageUrl = content.imageUrl, let url = URL(string: imageUrl) {
            cell = tableView.dequeueReusableCell(withIdentifier: "ImageContentRow", for: indexPath) as! ContentWithImageTableViewCell
            cell.imageView?.af_setImage(withURL: url)
        }else{
            cell = tableView.dequeueReusableCell(withIdentifier: "ContentRow", for: indexPath) as! ContentTableViewCell
        }
        cell.contentTitleVeiw.text = content.title
        cell.contentLeadView.text = content.lead
    }
    return cell!
}

图像视图单元格的视图层次结构如下所示:

启动应用并显示数据后,我的列表如下所示:

单击应显示图像的单元格或将其滚动并返回后,结果如下:

至少这是我的列表视图控制器,带有两个原型单元 (一种用于有图的内容,一种用于无图的内容)

【问题讨论】:

  • 我不得不说你的项目相当混乱。为什么在单元格内使用带有堆栈视图的内容视图?没明白。尝试按照本教程进行操作,它非常简单直接地做你需要的。 appcoda.com/customize-table-view-cells-for-uitableview
  • @GIJOW UIStackView 有什么问题?这就是我会做的方式..
  • 没有说问题,确实说令人困惑。我不会喜欢的。也许为什么我觉得它令人困惑
  • 感谢您的回答,我想尝试使用堆栈视图和约束来制作布局。起初我用堆栈视图实现它。 @GIJOW 我会看看你的链接。

标签: ios uitableview swift4 xcode9


【解决方案1】:

您需要通知tableView 图片已加载并且它必须重新绘制单元格。

尝试改变

cell.imageView?.af_setImage(withURL: url)

cell.imageView?.af_setImage(withURL: url, completion: { (_) in
    self.tableView.beginUpdates()
    self.tableView.setNeedsDisplay()
    self.tableView.endUpdates()
})

看看我的回答here - 最后你可能需要在imageView 上明确设置高度。

【讨论】:

  • 非常感谢!我现在没有时间,但我试过了,效果很好。我会读你的答案。
  • @user3057944 那么您介意对原始答案投赞成票吗?谢谢
  • 抱歉:忘记了。
  • @user3057944 我的意思是我提供链接的那个,但无论如何谢谢:D
  • 哦,我明白了!我也喜欢它。
猜你喜欢
  • 1970-01-01
  • 2011-07-09
  • 1970-01-01
  • 2011-09-10
  • 2016-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-18
相关资源
最近更新 更多