2010-10-07 64 views
2

我有一個自定義UITableViewCell這樣:自定義的UITableView細胞問題

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

細胞重複對自身每一個頂部的內容時間設備的方向改變。每當設備旋轉時是否重繪單元格?如果是這樣,我怎麼能阻止它呢?

回答

4

您應該創建子視圖並將它們添加到您的單元格的子視圖數組中,方法爲-initWithStyle:reuseIdentifier:。你只需要創建子視圖一次。每當設備方向發生變化時,框架會重複調用-layoutSubviews方法,以允許您調整和/或移動子視圖(以及進行任何其他微小調整),以補償方位之間的差異。

0

我懷疑layoutSubViews正在調用旋轉,並重新繪製新的標籤上的舊標籤。如果您從layoutSubViews的開始處的表格單元格中刪除任何可能會解決該問題的標籤,或者您可以在init中創建它們,然後將它們放在layoutSubViews中。