ios - 根据使用 SDWebImage 下载的图像高度更改 imageView 高度约束后 TableView 单元格未调整大小(AUTOLAYOUT)

标签 ios swift autolayout tableview sdwebimage

我正在使用自动布局。这是我在 cellForRowAtIndexPath 下使用它的代码:

cell.chatImage.sd_setImageWithURL(NSURL(string: newsFeed.postImage!), placeholderImage: UIImage(named: "default_profile"), completed: { (image, error, cacheType, imageURL) -> Void in

                let height = ((image?.size.height)! / (image?.size.width)!) * (tableView.bounds.width - 16)
                cell.chatImageHeight.constant = height
                self.view.layoutIfNeeded()
            })

单元格不会调整大小。它仅在滚动后调整大小。我应该怎么办? 因为我使用的是自动布局,所以我没有实现 heightForRowAtIndexPath。我已经提到了 UITableViewAutomaticDimensionestimatedRowHeight

最佳答案

我认为你必须调用 self.view.setNeedsLayout() 这将使整个 View 布局修复约束

cell.chatImage.sd_setImageWithURL(NSURL(string: newsFeed.postImage!), placeholderImage: UIImage(named: "default_profile"), completed: { (image, error, cacheType, imageURL) -> Void in

                let height = ((image?.size.height)! / (image?.size.width)!) * (tableView.bounds.width - 16)
                cell.chatImageHeight.constant = height

                self.view.setNeedsLayout()
                self.view.layoutIfNeeded()

            })

关于ios - 根据使用 SDWebImage 下载的图像高度更改 imageView 高度约束后 TableView 单元格未调整大小(AUTOLAYOUT),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37502294/

相关文章:

ios - 在第二次 UITextField 编辑时显示和隐藏键盘

iOS UIStepper 和 NSUserDefaults - 如何一起管理它们并使用双

ios - iTunes Connect 提交代码签名权利错误 Xcode 8

ios - 在快速递归插入和更新数据库中的数据时面临内存问题

swift - 是否可以制作 1 种布局并根据屏幕尺寸进行拉伸(stretch)?

ios - 自动布局的视觉格式不适用于 iOS8

ios - 应用程序更新时如何将启动屏幕文件更改为启动图像源?

ios - Facebook SDK 设置

ios - 如何传递函数作为上下文参数?

swift - 自动布局在调整大小的按钮下创建黑色空间