2010-10-07 2 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

Вы должны создавать подзоны и добавлять их в массив subviews вашей ячейки в вашем методе -initWithStyle:reuseIdentifier:. Вам нужно только создать подразделы один раз. Метод -layoutSubviews неоднократно вызывается каркасом всякий раз, когда изменяется ориентация устройства, чтобы вы могли изменять размеры и/или перемещать подсмотры (плюс делать любые другие незначительные корректировки), чтобы компенсировать различия между ориентациями.

0

Я подозреваю, что layoutSubViews вызывается при вращении и перерисовывает новые ярлыки поверх ваших старых. Если вы удалите любые метки из ячейки таблицы в начале layoutSubViews, которые, вероятно, исправит эту проблему, или вы можете создать их в init, а затем просто поместите их в layoutSubViews.

Смежные вопросы