【问题标题】:Auto resizing the height of TableViewCell to fit the UIImageVIew in iOS 6自动调整 TableViewCell 的高度以适应 iOS 6 中的 UIImageVIew
【发布时间】:2013-01-29 07:31:10
【问题描述】:

我有 uiimageview 属性来在我的表格视图中显示图像,并希望让一个特定的表格视图单元格根据 uiimageview 中图像的高度来更改其高度。下面是我所做的。不太确定为什么 tableview 单元格的高度没有改变... :(

@interface BoatViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate>

@property (nonatomic, weak) IBOutlet UIImageView *boatImageView;

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && indexPath.row == 0) {
        return self.boatImageView.image.size.height;
    }else{
        return 44;
    }
}

【问题讨论】:

    标签: ios uitableview uiimage


    【解决方案1】:

    在所有内容加载完毕后尝试重新加载表格视图数据。可能是在调用该方法时,iOS 还没有计算完框架的最终大小。

    在您的 viedDidAppear 方法中添加调用以重新加载 tableview。看看有没有帮助。

    【讨论】:

    • 所以我添加了 [self.tableview reloadData],但没有帮助。不确定这是否也是问题。我在viewDidLoad中将图片加载到uiimageview中,tableviewcell也没有正确设置高度...
    • 您正在加载的图像是远程图像,还是存储在设备上?如果它在设备上,那么 @Siba 建议尝试一下。那应该有希望解决:)
    【解决方案2】:

    试试这个代码希望它有帮助。

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        if (indexPath.section == 0 && indexPath.row == 0) {
    
        CGSize size = image.size; // Assign the image what you want to display.
    
      //  self.boatImageView.frame=CGRectMake(0, 0, size.width, size.height);
    
        return size.height;
    
        }else{
            return 44;
        }
    }
    

    【讨论】:

    • 您好,self.boatImageView.image.size.heightCGSize size = image.size 有什么区别?我认为两者都会给出 uiimage.image 大小的 CGFloat。
    • 嗨,在您的情况下,您正在 Imageview 中显示图像,然后您正在检查尺寸。完全相反。首先检查图像的大小,然后将该图像放入 imageview。如果你能展示你是如何获取图像的(从本地或远程),我可以更好地解释你
    • 啊哈,我明白你的意思了。所以,我一直在做的实际上是尝试使用 uiimageview 的高度而不是图像本身的大小。这解释了它。我明天试一试,然后告诉你。谢谢! :)
    猜你喜欢
    • 1970-01-01
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-20
    • 2013-06-05
    • 2016-03-18
    • 1970-01-01
    相关资源
    最近更新 更多