2016-12-28 3 views
2

私はUIImageViewを含むtableViewを持っていて、画像にURLがあれば画像が表示され、そうでなければ画像は表示されません。私が持っている問題は、画像がない場合、大きな空白のスポットが、画像があるかのようにTableViewに表示されることです。空白を減らす方法や隠す方法はありますか?イメージは中央の大きなUIImageViewです。それが戻ってくる文字列は2文字以上を持っているならば、それは有効なURLだとイメージがダウンロードされる基本的IOS SwiftどのようにTableView内の画像のスペースを非表示にすることができます

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 
} 

イメージに来るとき、これは私のコードです。私は、に焦点を当てています部分は、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

+1

を削除することにより、ゼロからセルを作成し、独自のコンテンツにビデオを適応させる最も簡単な方法(私はこのような画像の幅を0に変更することです。 – Axel

+1

imageViewの幅制約のコンセントを作成し、画像がない場合はその定数をゼロにします。 –

+1

イメージをアウトレット(カスタムイメージビュー)で使用する場合は、2つの異なるセルを作成できます。それ以外の場合は、イメージビューをランタイムとして使用する必要があります。 –

答えて

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

単に画像部分

関連する問題