2017-09-16 5 views
1

swift3을 사용하여 사용자가 그림이나 간단한 텍스트 게시물을 작성할 수있게하고 싶습니다. 전 단지 텍스트의 게시물을 만들 때를 제외하고는 모든 것이 잘 작동합니다. 셀의 UIImageView가 TableViewCell의 공간을 채 웁니다. 이상적으로, 사용자가 단지 텍스트의 포스트를 만들면 TableViewCell은 UIImageView가 아닌 ​​캡션 레이블까지 모든 것을 포함합니다 (이미지 참조). 이것에 대해 어떻게 생각하세요.TableViewCell의 이미지 크기를 동적으로 조정하십시오.

연구 :https://www.youtube.com/watch?v=zAWO9rldyUE, https://www.youtube.com/watch?v=TEMUOaamcDA, https://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

나는 경우에, 당신은 당신의 UI를 만들기 위해 스토리 보드를 사용하고 참조하면에 높이 제한을 추가 할 수 있습니다.(코드에서 사용하려면 셀에 연결해야합니다.) 필요할 때 제약 조건과 테이블 뷰의 높이를 변경하십시오.

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 
    } 
} 
관련 문제