2017-09-16 9 views
1

swift3を使用して、ユーザーがピクチャまたは簡単なテキスト投稿の投稿を作成できるようにします。私はちょうどテキストの投稿を作成する場合を除いて、すべてうまく動作しています。セルのUIImageViewは、TableViewCellのスペースを埋めます。理想的には、ユーザーが単なるテキストの投稿を作成する場合、TableViewCellはUIImageViewではなく、キャプションラベルまですべてを含みます(画像参照)。これについてどうすればいいですか?TableViewCellのサイズを動的に変更して画像を表示する場合と表示しない場合

研究:私はその場合、あなたには、あなたがあなたのUIを作成するために、ストーリーボードを使用している参照https://www.youtube.com/watch?v=zAWO9rldyUEhttps://www.youtube.com/watch?v=TEMUOaamcDAhttps://www.raywenderlich.com/129059/self-sizing-table-view-cells

現在のコード

func configureCell(post: Post){ 
    self.post = post 
    likesRef = FriendSystem.system.CURRENT_USER_REF.child("likes").child(post.postID) 
    userRef = FriendSystem.system.USER_REF.child(post.userID).child("profile") 

    self.captionText.text = post.caption 
    self.likesLbl.text = "\(post.likes)" 

    self.endDate = Date(timeIntervalSince1970: TimeInterval(post.time)) 

    userRef.observeSingleEvent(of: .value, with: { (snapshot) in 
     let snap = snapshot.value as? Dictionary<String, Any> 
     self.currentUser = MainUser(uid: post.userID, userData: snap!) 
     self.userNameLbl.text = self.currentUser.username 
     if let profileImg = self.currentUser.profileImage { 
      self.profileImg.loadImageUsingCache(urlString: profileImg) 
     } else { 
      self.profileImg.image = #imageLiteral(resourceName: "requests_icon") 
     } 
    }) 

    // This is where I belive I need to determine wether or not the cell should have an image or not. 
     if let postImg = post.imageUrl { 
      self.postImg.loadImageUsingCache(urlString: postImg) 
     } 

enter image description here

答えて

1

に高さの制約を追加できます(コード内で使用するにはセルに接続してください)、必要に応じて制約とテーブルビューの高さを変更してください。

class MyCell: UITableViewCell { 

    @IBOutlet var postImage: UIImageView! 
    @IBOutlet var postImageHeight: NSLayoutConstraint! 
} 


class ViewController: UITableViewController { 

    var dataSource: [Model] = [] 

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
     //Cell without image 
     if dataSource[indexPath.row].image == nil { 
      return 200 
     } 
     //Cell with image 
     return 350 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyCell 

     //Adjust the height constraint of the imageview within your cell 
     if dataSource[indexPath.row].image == nil { 
      cell.postImageHeight.constant == 0 
     }else{ 
      cell.postImageHeight.constant == 150 
     } 
     return cell 
    } 
} 
関連する問題