【发布时间】:2016-12-28 06:13:35
【问题描述】:
我有一个包含 UIImageView 的 tableView,如果 Image 有 URL,则显示图像,如果没有,则不显示图像。我遇到的问题是,如果没有图像,那么 TableView 中会出现一个大的空白点,就好像有图像一样。有没有办法减少空白点或隐藏它(下图)?该图像是中心的大 UIImageView 。这是我的图片代码
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyFeed", for: indexPath) as! MyFeed
if stream_image_string[indexPath.row].characters.count > 2 {
let strCellImageURL = self.stream_image_string[indexPath.row]
let imgURL: NSURL = NSURL(string: strCellImageURL)!
let request:NSURLRequest = NSURLRequest(url: imgURL as URL)
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let task = session.dataTask(with: request as URLRequest, completionHandler: {(data, response, error) in
DispatchQueue.main.async(execute: { () -> Void in
cell.post_image.image = UIImage(data: data!)
})
});
task.resume()
} else {
cell.post_image!.isHidden = true
cell.post_image!.image = nil
}
return cell
}
基本上,如果返回的字符串有 2 个或更多字符,那么它是一个有效的 URL,并且图像被下载;我关注的部分是 else 语句和这段代码
else {
cell.post_image!.isHidden = true
cell.post_image!.image = nil
}
显然,如果它进入 else 语句,则没有图像,我将 Image 设置为 null 或 nil,然后我尝试通过将 Image 设置为 hidden 来隐藏额外的空白,但这不起作用。关于如何隐藏空白的任何想法?我也一直在阅读这个问题,但它不起作用iOS swift imageView cannot be hidden in TableViewCell
【问题讨论】:
-
最简单的方法(我认为)是将图像的宽度更改为 0
-
为imageView的宽度约束创建一个outlet,在没有图片的情况下使其常量为零。
-
如果您想将图像与插座(自定义图像视图)一起使用,您可以创建两个不同的单元格,否则您应该使用图像视图作为运行时。
标签: ios swift uitableview uiimageview swift3