开发者

Custom uitableview cell issue

I have a custom UITableViewCell as such:

@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];

}

When I implement it in my UITableView like this:

- (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:UITableViewCellStyleDef开发者_如何学运维ault 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;
}

the contents of the cell duplicates on top of itself every time the orientation of the device changes. Is it redrawing the cell every time the device rotates? And if so, How can I keep it from doing so?


You should be creating the subviews and adding them to your cell's subviews array in your -initWithStyle:reuseIdentifier: method. You only need to create the subviews once. The -layoutSubviews method is invoked repeatedly by the framework whenever the device orientation changes, to allow you to resize and/or move the subviews (plus make any other minor adjustments) to compensate for differences between the orientations.


I suspect layoutSubViews is being called on rotation, and re-drawing new labels on top of your old ones. If you remove any labels from the table cell at the beginning of layoutSubViews that would probably fix that issue, or you could create them in the init and then just position them in layoutSubViews.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜