如何为UITableViewCell的ImageView设置固定大小?

时间:2013-01-25 10:24:44

标签: iphone ios cocoa-touch uitableview uiimageview

对于我的应用程序的联系人视图,我在表格视图中根据他的电子邮件ID添加了单个用户图像。由于每个用户的图像大小不同,图像视图并非对所有用户都是唯一的,一些图像视图的宽度更大,所以我设置了具有固定宽度的UItableViewCell的帧大小,但宽度大小仍然不同。

 [cell.imageView setFrame:CGRectMake(0, 0, 55, 55)];

我该怎么办?我是否需要将新图像视图添加为单元格的子视图?有什么想法吗?

4 个答案:

答案 0 :(得分:2)

UITableView的imageView属性是只读的。您可以在设置到单元格的imageView之前调整图像大小。要调整图片大小,您可以从StackOverflow answer获得帮助。

此外,如果您从Web环境加载图像,可以使用我的AFNetworking分支,我在UIImageView扩展中添加了图像大小调整选项。 https://github.com/polatolu/AFNetworking

答案 1 :(得分:1)

您应该设置imageview的属性,以便显示不同大小的不同图像。

例如,

<imageview_name>.contentMode = UIViewContentModeScaleAspectFit;

享受编程!

答案 2 :(得分:0)

UITableViewCell子类并覆盖此方法;

- (void)layoutSubviews {

    [super layoutSubviews];

    self.imageView.frame = CGRectMake(0,0,55,55);

    //Any additional setting that you want to do with image view

    [self.imageView setAutoresizingMask:UIViewAutoresizingNone];

}

答案 3 :(得分:0)

我使用相同的JSON文件来填充UITableView,这就是我在CustomCell中使用的通用应用程序:

- (void)layoutSubviews {
    [super layoutSubviews];
    if (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPad) {
        self.imageView.frame = CGRectMake(0,0,300,100);

    }else if  (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){
        self.imageView.frame = CGRectMake(0,0,180,60);
    }
    float limgW =  self.imageView.image.size.width;
    if(limgW > 0) {

        if (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPad) {
            self.textLabel.frame = CGRectMake(320,self.textLabel.frame.origin.y,self.textLabel.frame.size.width,self.textLabel.frame.size.height);

        }else if  (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){
            self.textLabel.frame = CGRectMake(182,self.textLabel.frame.origin.y,self.textLabel.frame.size.width,self.textLabel.frame.size.height);
        }

    }
}
相关问题