ios - 在 tableViewCell 内实现 UIImageView 动态单元高度的最佳方法?

标签 ios objective-c uitableview uiimageview

试图拥有平滑的动态高度,目前它可以工作,但它从一个尺寸到动态尺寸出现故障,看起来不太好。

这是我在 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 的高度约束,然后相应地调整了单元格的大小。但还是有问题。

有没有更好的办法?

最佳答案

您可以通过在 viewDidLoadautolayout 中添加以下行来解决您的问题。

_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

http://candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/

希望对你有所帮助!

关于ios - 在 tableViewCell 内实现 UIImageView 动态单元高度的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37825939/

相关文章:

ios - 如何检索当前控制状态栏外观的 View Controller ?

ios - 无限滚动或其他一些技术?

ios - Alamofire 网络调用未在后台线程中运行

iphone - 以特定方法访问NSManagedObjects时,应用程序崩溃

ios - 在 UITableView 中设置复选标记

ios - 动态设置桌面 View 高度并限制在一定范围内 swift 3?

ios - 如何在 Swift 3 中将 UIButton 添加到我的 UITableViewCell 中?

ios - 范围不正确

ios - 如果用户禁用了应用程序的推送,是否可以进行静默远程通知?

objective-c - 我应该将我的事件指标发布声明放在 dealloc 中吗?