【发布时间】: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