更改UITableViewCell的imageView边界

时间:2010-06-28 07:54:36

标签: iphone uitableview imageview

我正在尝试将各种大小的图像放在UITableViewCell的imageView中。我得到图像数据asynch'ly,创建图像,设置imageView的内容模式,最后设置imageView的边界。但是代码似乎对我所做的任何更改都不敏感。我希望图像以75x75的区域为中心。我为此目的编写了以下代码

UIImage* image = [UIImage imageWithData:data];
[holder.imageView setContentMode:UIViewContentModeCenter || UIViewContentModeRedraw];
[holder.imageView setImage:image];
[holder.imageView setBounds:CGRectMake(0,0,75,75)];
[holder.imageView setFrame:CGRectMake(0,0,75,75)];
[holder setNeedsLayout]; 

持有者是UITableViewCell。我得到的结果总是一样的。所有图像都有75px的高度和不同的宽度。有人可以帮我解决这个问题吗?

我已经意识到设置contentMode和bounds属性对该代码没有任何影响。我在最后一行之后添加了一个NSLog,得到如下结果:

NSLog(@"imageview:%@ bounds and contentMode:%@ %@",[holder imageView],[holder.imageView bounds],[holder.imageView contentMode]);
  

imageview:< UIImageView:0x39ab8a0;   frame =(0 0; 75 75); opaque = NO;   userInteractionEnabled = NO; layer =   < CALayer:0x39a92b0>>边界和   contentMode:(null)(null)

仍然没有解决方案

5 个答案:

答案 0 :(得分:42)

完成后,我终于找到了解决方案,虽然我花了3个小时=)

解决方案是更改自定义UITableViewCell类的 - (void)layoutSubviews方法中的bound,frame,contentMode等属性。 “技巧”是在此方法中编写布局代码,否则代码没有任何影响。

下面的代码为我做了工作。它使表格的行垂直对齐。

- (void)layoutSubviews {
    [super layoutSubviews];
    self.imageView.bounds = CGRectMake(0,0,75,75);
    self.imageView.frame = CGRectMake(0,0,75,75);
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;

    CGRect tmpFrame = self.textLabel.frame;
    tmpFrame.origin.x = 77;
    self.textLabel.frame = tmpFrame;

    tmpFrame = self.detailTextLabel.frame;
    tmpFrame.origin.x = 77;
    self.detailTextLabel.frame = tmpFrame;

}

答案 1 :(得分:11)

所以 UITableViewCell 的问题在于你无法控制内置对象的大小(即 imageView,contentView,accessoryView,backgroundView )。当表格更改时,您的自定义设置会被践踏。

正如Behlul指出的那样,您可以使用 layoutSubviews 强制大小正确,但问题是每次表滚动时都会调用layoutSubviews。这是很多不必要的重新布局调用。

另一种方法是将所有内容添加到 contentView 。同样,如果您要自定义背景,则可以创建透明的 backgroundView 并添加自定义背景视图(例如 myBackgroundView )作为 backgroundView 的子视图

通过这种方式,您可以根据需要放置和调整项目大小。

缺点是不再从附件或图像视图中接收股票消息。你只需创建自己的。

希望有所帮助!

// This code is not tested
// MyCustomTableViewCell
- (id) init{
    self = [super initWithStyle: UITableViewCellStyleDefault reuseIdentifier:@"MyReuseIdentifier"];
    if(self){
        //image view
        my_image_view = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"default_image.png"]] retain];
        [my_image_view setFrame:CGRectMake(10,10,30,30)];
        [self.contentView addSubview:my_image_view];

        //labels
        my_text_label = [[[UILabel alloc] initWithFrame:CGRectMake(50,10,100,15)] retain];
        [self.contentView addSubview:my_text_label];
        //set font, etc

        //detail label
        my_detail_label = [[[UILabel alloc] initWithFrame:CGRectMake(50,25,100,15)] retain];
        [self.contentView addSubview:my_detail_label];
        //set font, etc

        //accessory view
        //Whatever you want to do here
        //attach "accessoryButtonTapped" selector to button action

        //background view
        UIView* background_view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 50)] autorelease];
        [background_view setBackgroundColor:[UIColor greenColor]];
        background_view.layer.cornerRadius = 17;
        background_view.layer.borderWidth = 3;
        background_view.layer.borderColor = [UIColor whiteColor].CGColor;

        [self setBackgroundView:[[[UIView alloc] init] autorelease]];
        [self.backgroundView addSubview:background_view];
    }

    return self;
}

- (void) setLabelText: (NSString*) label_text{
    [my_text_label setText:label_text];
}

- (void) setDetailText: (NSString*) detail_text{
    [my_detail_label setText: detail_text];
}

- (void) accessoryButtonTapped{
    //call table view delegate's accessoryButtonTappedForRowWithIndexPath method
}

答案 2 :(得分:2)

“UIViewContentModeCenter || UIViewContentModeRedraw”相当于1.它也不是位域。你想要UIViewContentModeCenter。

UITableViewCell.imageView由单元管理。如果你想要自定义布局,请尝试向contentView添加一个视图(我猜你的意思是“以75x75区域为中心”):

UIImageView * iv = [[[UIImageView alloc] initWithImage:image] autorelease];
iv.frame = (CGRect){{0,0},{75,75}};
iv.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin| UIViewAutoresizingFlexibleRightMargin;
iv.contentMode = UIViewContentModeScaleAspectFit;
[holder.contentView addSubview:iv];

答案 3 :(得分:1)

尝试将imageView的“contentMode”属性更改为“UIViewContentModeScaleAspectFit”或“UIViewContentModeScaleAspectFill”

答案 4 :(得分:0)

创建UITableViewCell的子类:

@interface UITableViewCellSubClass : UITableViewCell

@end

@implementation UITableViewCellSubClass

- (void)layoutSubviews {
    [super layoutSubviews];
    self.imageView.frame = CGRectMake(0,4,32,32);
    self.textLabel.frame = CGRectMake(42,4,300,32);
}
@end
相关问题