我有一个包含UIImageView的tableView,如果图像有一个URL,那么图像显示,如果没有图像显示。我遇到的问题是,如果没有Image,那么TableView中会出现一个大的空白点,就像有图像一样。有没有办法减少空白点或隐藏它(下面的图片)?该图像是中央的大UIImageView。这是我的代码,当涉及到的图像IOS Swift如何隐藏表格视图中的图像空间
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语句所以,很显然那么有没有像我设置图片为空或零,然后我试图隐藏通过将图像设置为隐藏但额外的空白空间不起作用。有关我如何隐藏白色空间的任何想法?我也一直在阅读这个问题,但它不工作iOS swift imageView cannot be hidden in TableViewCell
最简单的方法(我认为)是将图像的宽度更改为0 – Axel
为imageView的宽度约束创建出口,并在没有图像时将其固定为零。 –
如果您想使用带有插座的图像(自定义图像视图),则可以创建两个不同的单元格,否则应该使用图像视图作为运行时。 –