[英]Custom uitableview cell issue
我有一個自定義的UITableViewCell
這樣:
@implementation CellWithThreeSubtitles
@synthesize title, subTitle1, subTitle2, subTitle3;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.textLabel.backgroundColor = self.backgroundColor;
self.textLabel.opaque = NO;
self.textLabel.textColor = [UIColor blackColor];
self.textLabel.highlightedTextColor = [UIColor whiteColor];
self.textLabel.font = [UIFont boldSystemFontOfSize:22.0];
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGRect frame = CGRectMake(35.0, 0, contentRect.size.width, 50.0);
self.textLabel.frame = frame;
UILabel *subLabel1 = [[UILabel alloc] init];
frame = CGRectMake(35.0, 34.0, contentRect.size.width, 25.0);
subLabel1.font = [UIFont systemFontOfSize:18.0];
subLabel1.backgroundColor = [UIColor clearColor];
subLabel1.text = subTitle1;
subLabel1.opaque = NO;
subLabel1.frame = frame;
[self.contentView addSubview:subLabel1];
UILabel *subLabel2 = [[UILabel alloc] init];
frame = CGRectMake(35.0, 54.0, contentRect.size.width, 25.0);
subLabel2.font = [UIFont boldSystemFontOfSize:18.0];
subLabel2.backgroundColor = [UIColor clearColor];
subLabel2.text = subTitle2;
subLabel2.opaque = NO;
subLabel2.frame = frame;
[self.contentView addSubview:subLabel2];
UILabel *subLabel3 = [[UILabel alloc] init];
frame = CGRectMake(contentRect.size.width-100.0, 54.0, contentRect.size.width, 25.0);
subLabel3.font = [UIFont systemFontOfSize:18.0];
subLabel3.backgroundColor = [UIColor clearColor];
subLabel3.text = subTitle3;
subLabel3.opaque = NO;
subLabel3.frame = frame;
[self.contentView addSubview:subLabel3];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)dealloc {
[super dealloc];
[subTitle4 release];
[subTitle3 release];
[subTitle2 release];
[subTitle1 release];
[title release];
}
當我像這樣在UITableView
實現它時:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
int section = [indexPath indexAtPosition:0];
static NSString *kCustomCellID = @"AppointmentCellID";
CellWithThreeSubtitles *cell = (CellWithThreeSubtitles *)[tableView dequeueReusableCellWithIdentifier:kCustomCellID];
if (cell == nil) {
cell = (CellWithThreeSubtitles *)[[[CellWithThreeSubtitles alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease];
}
switch (section) {
case 0:
if ([wineArray count]==0) {
cell.textLabel.text = @"No wine favorites selected yet";
}else{
cell.subTitle1 = [[wineArray objectAtIndex:indexPath.row] objectForKey:@"Varietal"];
cell.subTitle2 = [NSString stringWithFormat:@"Bin No. %@", [[wineArray objectAtIndex:indexPath.row] objectForKey:@"Bin"]];
cell.subTitle3 = [NSString stringWithFormat:@"$%@", [[wineArray objectAtIndex:indexPath.row] objectForKey:@"Price"]];
cell.textLabel.text = [NSString stringWithFormat:@"%@ (%@)", [[wineArray objectAtIndex:indexPath.row] objectForKey:@"Name"], [[wineArray objectAtIndex:indexPath.row] objectForKey:@"Availability"]];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.userInteractionEnabled = NO;
break;
case 1:
if ([dinnerArray count]==0)
cell.textLabel.text = @"No dinner favorites selected yet";
else
cell.textLabel.text = [NSString stringWithFormat:@"%@", [[dinnerArray objectAtIndex:indexPath.row] objectForKey:@"Name"]];
break;
}
return cell;
}
每次設備方向更改時,單元格的內容都會在其自身的頂部復制。 每次設備旋轉時,它都會重繪單元格嗎? 如果是這樣,我如何才能避免這樣做?
您應該在-initWithStyle:reuseIdentifier:
方法中創建子視圖並將其添加到單元格的子視圖數組中。 您只需要創建一次子視圖。 每當設備方向更改時,框架都會反復調用-layoutSubviews
方法,以允許您調整和/或移動子視圖的大小(以及進行任何其他較小的調整)以補償方向之間的差異。
我懷疑layoutSubViews在旋轉時被調用,並在舊標簽之上重新繪制新標簽。 如果您從layoutSubViews開頭的表格單元格中刪除了可能會解決該問題的任何標簽,或者可以在init中創建它們,然后將其放置在layoutSubViews中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.