自定义uitableview单元格问题

时间:2010-10-07 18:31:37

标签: iphone objective-c uitableview

我有一个自定义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;
}

每次设备的方向改变时,单元格的内容都会在其自身的顶部复制。每次设备旋转时是否重新绘制单元格?如果是这样,我怎么能阻止它这样做呢?

2 个答案:

答案 0 :(得分:4)

您应该创建子视图并将其添加到-initWithStyle:reuseIdentifier:方法中的单元格子视图数组中。您只需要创建一次子视图。每当设备方向改变时,框架都会重复调用-layoutSubviews方法,以允许您调整和/或移动子视图(以及进行任何其他微小调整)以补偿方向之间的差异。

答案 1 :(得分:0)

我怀疑在旋转时调用layoutSubViews,并在旧的标签上重新绘制新标签。如果从layoutSubViews开头的表格单元格中删除任何可能解决该问题的标签,或者您可以在init中创建它们,然后将它们放置在layoutSubViews中。