IOS Swift 如何隐藏 TableView 内图像的空间

标签 ios swift uitableview uiimageview swift3

我有一个包含 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

Big Image in the center

enter image description here

最佳答案

给出图像宽度的导出,如果没有图像,则将该导出的常量设置为“0”。

例如 如果(!图像) {

图像宽度.constant = 0

}

关于IOS Swift 如何隐藏 TableView 内图像的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41356364/

相关文章:

ios - 如何从 iOS 中的后台任务打开应用程序?

ios - XIB 中的淡入/淡出 UISegmentedControl

ios - 找不到接受提供的参数的 '!=' 的重载?

ios - 核心数据加载 Swift 后异步刷新 UITableView

ios - 使用核心数据将联系人保存到 TableView 中

iphone - 鉴于此代码,为什么后退按钮不会出现(UINavigationController 问题)

ios - 使用来自包含 block 的两个方法的信息删除 UITableViewRows

ios - 使用导航 Controller 在 Storyboard 中呈现 View Controller - Swift

ios - 在 Objective-C 中使用通用的 Swift 类

ios - uitableview 标题中的单独点击手势识别器