2016-12-28 124 views
2

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

Big Image in the center

enter image description here

+1

最简单的方法(我认为)是将图像的宽度更改为0 – Axel

+1

为imageView的宽度约束创建出口,并在没有图像时将其固定为零。 –

+1

如果您想使用带有插座的图像(自定义图像视图),则可以创建两个不同的单元格,否则应该使用图像视图作为运行时。 –

回答

2

给图像的宽度的出口,如果没有图像,然后设定常数的出口为“0”的。

例如 如果(!图片) {

widthOfImage.constant = 0

}

0

我经历了同样的问题我自己。你需要为此创建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

适应视频以它自己的内容,从头开始创建一个细胞通过简单地删除图像部分

相关问题