【问题标题】:IOS Swift how can hide the space of an Image inside a TableViewIOS Swift如何隐藏TableView中的图像空间
【发布时间】: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


【解决方案1】:

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

例如 如果(!图像) {

widthOfImage.constant = 0

}

【讨论】:

    【解决方案2】:

    我自己也遇到过同样的问题。您需要为此创建 2 个单元格。像这样:

    override func tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if (stream_image_string[indexPath.row] == "")
         {
           let cell = tableView.dequeueReusableCell (withIdentifier: "noImageMyFeed", for: indexPath) as! noImageMyFeed
           return cell
    
         }
        else
         {
           let cell = tableView.dequeueReusableCell (withIdentifier: "MyFeed", for: indexPath) as! MyFeed
           return cell
         }
        }
    

    此视频将详细帮助您:https://www.youtube.com/watch?v=FAxtWtqeMIM

    根据自己的内容调整视频,只需删除图像部分即可从头开始创建单元格

    【讨论】:

      猜你喜欢
      • 2021-05-28
      • 2016-01-18
      • 1970-01-01
      • 2017-06-15
      • 1970-01-01
      • 1970-01-01
      • 2015-08-18
      • 2016-02-13
      • 2016-05-13
      相关资源
      最近更新 更多