Ios 使用和不使用图像动态调整TableViewCell的大小
使用swift3,我想允许用户创建图片或简单文本帖子。除了创建一篇只包含文本的文章时,单元格中的UIImageView会填充TableViewCell中的空间外,其他一切都正常工作。理想情况下,如果用户仅创建一篇文本文章,TableViewCell将只包含标题标签下的所有内容,而不包括UIImageView(见图)。我该怎么办呢 研究:, , 当前代码Ios 使用和不使用图像动态调整TableViewCell的大小,ios,swift,uitableview,Ios,Swift,Uitableview,使用swift3,我想允许用户创建图片或简单文本帖子。除了创建一篇只包含文本的文章时,单元格中的UIImageView会填充TableViewCell中的空间外,其他一切都正常工作。理想情况下,如果用户仅创建一篇文本文章,TableViewCell将只包含标题标签下的所有内容,而不包括UIImageView(见图)。我该怎么办呢 研究:, , 当前代码 func configureCell(post: Post){ self.post = post likesRef =
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)
}
func配置单元(post:post){
self.post=post
likesRef=FriendSystem.system.CURRENT\u USER\u REF.child(“likes”).child(post.postID)
userRef=FriendSystem.system.USER\u REF.child(post.userID.child)(“配置文件”)
self.captionText.text=post.caption
self.likesLbl.text=“\(post.likes)”
self.endDate=日期(timeIntervalSince1970:TimeInterval(post.time))
userRef.observeSingleEvent(的值为:),其中:{(快照)位于
设snap=snapshot.value为字典
self.currentUser=MainUser(uid:post.userID,userData:snap!)
self.userNameLbl.text=self.currentUser.username
如果让profileImg=self.currentUser.profileImage{
self.profileImg.loadImageUsingCache(urlString:profileImg)
}否则{
self.profileImg.image=#imageLiteral(resourceName:“请求图标”)
}
})
//这就是我认为我需要确定细胞是否应该有图像的地方。
如果让postImg=post.imageUrl{
self.postImg.loadImageUsingCache(urlString:postImg)
}
我看到您正在使用故事板创建UI,在这种情况下,您可以将高度约束添加到
UIImageView
(确保将其连接到单元格以便在代码中使用),并在需要时更改约束和tableview的高度
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
}
}