2016-12-28 53 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

適應視頻以它自己的內容,從頭開始創建一個細胞通過簡單地刪除圖像部分

相關問題