2010-06-28 32 views
10

我试图在UITableViewCell的imageView内放置各种尺寸的图像。我同步获取图像数据,创建图像,设置imageView的内容模式,最后设置imageView的边界。但代码似乎对我所做的任何更改都不敏感。我希望图像以75x75的区域为中心。我为此写了下面的代码更改imageView的UITableViewCell的边界

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]; 

其中holder是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;层= <的CALayer:0x39a92b0 >>边界和 contentMode:(空)(空)

仍然没有解决

回答

42

完成,我终于找到了解决方案,它的成本我3小时虽然=)

的解决方案是改变像结合,框架属性,contentMode在 - (无效)layoutSubviews我自定义UITableViewCell类的方法。 “技巧”是用这种方法编写布局代码,否则代码不起作用。

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

- (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; 

} 
+0

Bummer,我想我可能会被迫创建另一个UITableViewCell子类...感谢您为我确认它! – 2012-10-04 22:13:01

+1

我必须同意,这是一个巨大的浪费时间,以使其按预期工作。 – 2013-08-16 12:51:44

+0

虽然这个工作,它仍然是一个黑客。人们会期望从自定义UITableViewCell中设置的约束中找出框架/边界。 – 2014-04-12 20:15:20

1

尝试改变的ImageView的 “contentMode” 属性为 'UIViewContentModeScaleAspectFit' 或 'UIViewContentModeScaleAspectFill'

+1

谢谢你,我想你的建议,但没有奏效。我意识到,无论我设置为contentMode和bounds的值如何,都没有任何影响。我编辑了我的第一篇文章来反映这一点。 – 2010-06-28 10:10:50

2

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

UITableViewCell.imageView由单元管理。如果你想自定义布局,尝试将着眼于内容查看(我猜你所说的“集中在一个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]; 
+0

谢谢!我没有使用内容视图解决了问题。我会将问题标记为2天后回答=) – 2010-06-28 13:13:52

10

因此,与的UITableViewCell的问题是,你有无法控制内置对象的大小(即imageView,contentView,accessoryView,backgroundView)。当表格发生变化时,您的自定义会被践踏。

您可以像Behlul指出的那样,通过使用layoutSubviews来强制尺寸正确,但问题在于每次滚动表格时都会调用layoutSubviews。这是很多不必要的重新布局调用。

另一种方法是将所有的内容添加到contentView。同样,如果您正在自定义背景,则可以创建一个透明的背景视图,并将您的自定义背景视图(例如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 
} 
+0

这实际上是比选定答案更好的方法。如果你愿意,你也可以通过xib创建一个UITableViewCell。这里有一个关于它的教程:http://ijoshsmith.com/2011/07/16/creating-a-custom-uitableviewcell-in-ios-4/ – 2012-10-04 22:14:52

+0

这为我完成了诀窍。 – 2014-12-09 08:51:06

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