我想在一個iPhone應用程序的分組表格視圖中自定義UITableViewCell。我想要做的是圖像寬度佔用整個單元格減去填充(280)和基於圖像大小的高度變量。根據圖像大小調整自定義的UITableViewCell和包含的UIImageView
目前我正在使用SDWebImage異步下載遠程圖像。在這種情況下,這可能不是正確的做法。我也很難弄清楚如何在初始化時給定製單元格圖像。圖像URL存儲在DetailViewController的self.beerPhoto中。
我已經搜索了很多方法,還沒有找到我正在尋找什麼。最近的是這樣的:How to scale a UIImageView proportionally,但是這種方法似乎要求單元在初始化時有圖像,因爲我試圖讓這個代碼工作,但是在初始化後設置圖像留下了一個空單元。
當前代碼包含我設置的用於近似縱向圖像的常量。實際上,一些圖像是人像,一些是橫向。
請讓我知道是否有什麼額外的,你需要知道的。
爲自定義的UITableViewCell頭:實施了自定義的UITableViewCell
#import "BeerDetailHead.h"
@implementation BeerDetailHead
@synthesize beerName, beerImage;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
//beerName = [[UILabel alloc]init];
//beerName.textAlignment = UITextAlignmentLeft;
//beerName.font = [UIFont systemFontOfSize:14];
beerImage = [[UIImageView alloc]init];
//[self.contentView addSubview:beerName];
[self.contentView addSubview:beerImage];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame= CGRectMake(boundsX+10 ,10, 280, 375);
beerImage.frame = frame;
}
DetailViewController的的cellForRowAtIndexPath
#import <UIKit/UIKit.h>
@interface BeerDetailHead : UITableViewCell {
UILabel *beerName;
UIImageView *beerImage;
}
@property(nonatomic, retain)UILabel *beerName;
@property(nonatomic, retain)UIImageView *beerImage;
@end
相關部分
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
NSArray *listData =[self.tableContents objectForKey:
[self.sortedKeys objectAtIndex:[indexPath section]]];
NSLog(@"listData = %@", listData);
NSUInteger row = [indexPath row];
if ([self.sortedKeys objectAtIndex:[indexPath section]] == @"header"){
static NSString *headerTableIdentifier = @"HeaderTableIdentifier";
BeerDetailHead * headerCell = (BeerDetailHead*)[tableView
dequeueReusableCellWithIdentifier: headerTableIdentifier];
if(headerCell == nil) {
headerCell = [[[BeerDetailHead alloc] initWithFrame:CGRectZero reuseIdentifier:headerTableIdentifier] autorelease];
}
headerCell.beerName.text = [listData objectAtIndex:row];
[headerCell.beerImage setImageWithURL:[NSURL URLWithString:self.beerPhoto]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
//NSLog(@"frame = %@", headerCell.beerImage.frame);
return headerCell;
}
else{
//use standard UITableViewCell
}
}
我有tableView:heightForRowAtIndexPath實現一個常量;忘了在這裏列出。截至目前,自定義UITableViewCell使用圖像的寬度,高度和常量調整後的寬度280來計算UIImageView框架的正確高度。在viewDidAppear中調用reloadData可提供正確的圖像。但是,heightForRowAtIndexPath是在圖像具有正確高度之前計算的。仍然在爲高度制定合適的解決方案。 – scottoliver
只是好奇你爲什麼有點拖延?有什麼特別的原因嗎? – fatih