试图拥有平滑的动态高度,目前它可以工作,但它从一个尺寸到动态尺寸出现故障,看起来不太好。
这是我在 cellForRowAtIndexPath
中的代码
cell.productImageView.file = (PFFile *)object[@"image"];
[cell.productImageView loadInBackground:^(UIImage * _Nullable image, NSError * _Nullable error) {
cell.imageHeightConstraint.constant = image.size.height / image.size.width * cell.cardView.frame.size.width;
[cell.productImageView setNeedsUpdateConstraints];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
if (indexPath != nil) {
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
[UIView animateWithDuration:0.50f animations:^{
[cell.cardView layoutIfNeeded];
}];
}];
如您所见,我只是更新了 ImageView 的高度约束,然后相应地调整了单元格的大小。但还是有问题。
有没有更好的办法?
最佳答案
您可以通过在 viewDidLoad
和 autolayout
中添加以下行来解决您的问题。
_tblView.delegate = self;
_tblView.dataSource = self;
self._tblView.estimatedRowHeight = 80; // The estimatedRowHeight but if is more this autoincremented with autolayout
self._tblView.rowHeight = UITableViewAutomaticDimension;
[self._tblView setNeedsLayout];
[self._tblView layoutIfNeeded];
self._tblView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0) ;
这里是更多信息:
https://www.raywenderlich.com/87975
希望对你有所帮助!
关于ios - 在 tableViewCell 内实现 UIImageView 动态单元高度的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37825939/