2010-06-28 70 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