したがって、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
}