我有一个包含 UIImageView 的 tableView,如果图像有 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 语句中,那么就没有图像,并且我将图像设置为 null 或 nil,然后我尝试通过将图像设置为隐藏来隐藏额外的空白,但这不起作用。关于如何隐藏空白有什么想法吗?我也一直在阅读这个问题,但它不起作用iOS swift imageView cannot be hidden in TableViewCell
最佳答案
给出图像宽度的导出,如果没有图像,则将该导出的常量设置为“0”。
例如 如果(!图像) {
图像宽度.constant = 0
}
关于IOS Swift 如何隐藏 TableView 内图像的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41356364/