【问题标题】:TableView cell not resizing after changing imageView height constraint based on Image height downloaded using SDWebImage (AUTOLAYOUT)基于使用 SDWebImage (AUTOLAYOUT) 下载的图像高度更改 imageView 高度约束后 TableView 单元格未调整大小
【发布时间】:2016-05-28 18:07:12
【问题描述】:

我正在使用自动布局。这是我在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

【问题讨论】:

  • 等等,你是否使用了uitableviewautomaticdimension?我不确定你在哪里提到的。
  • @user3353890 我已经提到它确实加载了。只有当我尝试更新块内的约束时才会出现问题。

标签: ios swift autolayout tableview sdwebimage


【解决方案1】:

我认为你必须调用 self.view.setNeedsLayout() 这将使整个视图布局固定约束

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()

            })

【讨论】:

  • 嗯,你能增加estimatedRowHeight或试试这个应该可以工作-------------------------- -------------- cell.chatImageHeight.constant = height UIView.animateWithDuration(0.3, 动画: cell.contentView.layoutIfNeeded) tableView?.beginUpdates() tableView?.endUpdates()
  • 我试过 tableview.beginUpdates 和 endUpdates 但这不是正确的方法,应用程序最终会崩溃,因为它在 indexPath 的行中被一次又一次地调用。
  • 有什么消息吗?我面临同样的问题:/
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-22
  • 2016-03-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多