2012-05-04 2 views
0

Я разрабатываю универсальное приложение. В этом приложении есть UITableView. Проблема в том, что строка заголовка в TableView повторяется после 6 строк в iphone и 9 строк в iPad.Iphone: строка заголовка UITableView, повторяющаяся после 6 строк

Я googled, но не нашел решение для решения этой проблемы с заголовком. пожалуйста помоги.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 

    return [tripList count]; 
} 

UITableView код cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UILabel *lblTripNumber = nil; 
    UILabel *lblTripState = nil; 
    UIImageView *imgTripState = nil; 
    UILabel *lblStateTitleText = nil; 
    UILabel *lblTripNUmberTitleValue = nil; 

    static NSUInteger const kTripNumberLabelTag = 2; 
    static NSUInteger const kTripStateLabelTag = 3; 
    static NSUInteger const kTripImageTag = 4; 


    BOOL isiPhone = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(indexPath.row == 0) 
    { 
     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

      if (!isiPhone) // 1=Ipad   
      { 
       lblStateTitleText = [[[UILabel alloc] initWithFrame:CGRectMake(205, 27, 250, (cell.contentView.frame.size.height))] autorelease]; 
       lblStateTitleText.font = [UIFont boldSystemFontOfSize:32]; 
      } 
      else 
      { 
       lblStateTitleText = [[[UILabel alloc] initWithFrame:CGRectMake(65, 10, 130, (cell.contentView.frame.size.height))] autorelease]; 
       lblStateTitleText.font = [UIFont boldSystemFontOfSize:16]; 
      } 
      lblStateTitleText.textAlignment = UITextAlignmentLeft; 
      lblStateTitleText.backgroundColor = [UIColor clearColor]; 
      lblStateTitleText.tag = 11;  
      lblStateTitleText.text = @"Trip State"; 
      lblStateTitleText.textColor = [UIColor cyanColor]; 
      [cell.contentView addSubview:lblStateTitleText]; 

      if (!isiPhone) // 1=Ipad   
      { 
       lblTripNUmberTitleValue = [[[UILabel alloc] initWithFrame:CGRectMake(445, 27, 290, (cell.contentView.frame.size.height))] autorelease]; 
       lblTripNUmberTitleValue.font = [UIFont boldSystemFontOfSize:32]; 
      } 
      else 
      { 
       lblTripNUmberTitleValue = [[[UILabel alloc] initWithFrame:CGRectMake(180, 10, 250, (cell.contentView.frame.size.height))] autorelease]; 
       lblTripNUmberTitleValue.font = [UIFont boldSystemFontOfSize:16]; 
      } 
      lblTripNUmberTitleValue.textAlignment = UITextAlignmentLeft; 
      lblTripNUmberTitleValue.backgroundColor = [UIColor clearColor]; 
      lblTripNUmberTitleValue.tag = 12;  
      lblTripNUmberTitleValue.text = @"Confirmation#";  
      lblTripNUmberTitleValue.textColor = [UIColor greenColor]; 
      [cell.contentView addSubview:lblTripNUmberTitleValue]; 

     } 

    } 
    else 
    { 
     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

      CGFloat xIndex = 6.0;  
      CGFloat xIpadIndex = 6.0; 

      CGFloat TripNumberLabelWidth = 130.0; 

      if (!isiPhone) // 1=Ipad   
      { 
       imgTripState = [[[UIImageView alloc] initWithFrame:CGRectMake(xIndex, 18, 50, 64)] autorelease]; 
      } 
      else 
      { 
       imgTripState = [[[UIImageView alloc] initWithFrame:CGRectMake(xIndex, 8, 32, 32)] autorelease]; 
      } 

      imgTripState.tag = kTripImageTag; 
      [cell.contentView addSubview:imgTripState]; 

      xIndex +=73; 
      xIpadIndex +=85; 

      if (!isiPhone) // 1=Ipad   
      { 
       lblTripState = [[[UILabel alloc] initWithFrame:CGRectMake(xIpadIndex, 25, TripNumberLabelWidth, (cell.contentView.frame.size.height))] autorelease]; 
       lblTripState.font = [UIFont boldSystemFontOfSize:38]; 
      } 
      else 
      { 
       lblTripState = [[[UILabel alloc] initWithFrame:CGRectMake(xIndex, 1, TripNumberLabelWidth, (cell.contentView.frame.size.height))] autorelease]; 
       lblTripState.font = [UIFont boldSystemFontOfSize:20]; 
      } 
      lblTripState.textAlignment = UITextAlignmentLeft; 
      lblTripState.backgroundColor = [UIColor clearColor]; 
      lblTripState.tag = kTripStateLabelTag;  
      lblTripState.textColor = [UIColor colorWithRed:(0.0/255) green:(241.0/255) blue:(216.0/255) alpha:1.0f]; 
      [cell.contentView addSubview:lblTripState]; 

      xIndex +=132; 
      xIpadIndex +=120; 

      if (!isiPhone) // 1=Ipad 
      { 
       lblTripNumber = [[[UILabel alloc] initWithFrame:CGRectMake(xIpadIndex, 25, TripNumberLabelWidth, (cell.contentView.frame.size.height))] autorelease]; 
       lblTripNumber.font = [UIFont boldSystemFontOfSize:35]; 
      } 
      else 
      { 
       lblTripNumber = [[[UILabel alloc] initWithFrame:CGRectMake(xIndex, 0, TripNumberLabelWidth, (cell.contentView.frame.size.height))] autorelease]; 
       lblTripNumber.font = [UIFont boldSystemFontOfSize:18]; 
      } 

      lblTripNumber.textAlignment = UITextAlignmentLeft; 
      lblTripNumber.backgroundColor = [UIColor clearColor]; 
      lblTripNumber.tag = kTripNumberLabelTag;   
      lblTripNumber.textColor = [UIColor greenColor]; 
      [cell.contentView addSubview:lblTripNumber]; 

     }else 
     { 
      // A reusable cell was available, so we just need to get a reference to the subviews using their tags. 
      lblTripNumber = (UILabel *)[cell.contentView viewWithTag:kTripNumberLabelTag]; 
      lblTripState = (UILabel *)[cell.contentView viewWithTag:kTripStateLabelTag]; 
     // imgTripState = (UILabel *)[cell.contentView viewWithTag:kTripImageTag]; 
     } 

     lblTripNumber.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 
     lblTripState.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 


     int indCount = indexPath.row -1; 
     TripDetails *trip = [tripList objectAtIndex:indCount]; 
     lblTripNumber.text = trip.tripNumber; 
     lblTripState.text = trip.tripState; 


    // Configure the cell... 

    return cell; 
} 
+0

введите код зрения для администратора в раздел и номер раздела – Saad

ответ

1

я получил, что я удалил

if (cell == nil) 

и он работал отлично. Фактически он искал свои метки в первой строке заголовка и не мог заменить его значения, поэтому создавала проблему.

+0

Я делаю то же самое, но это сбой моего приложения –

1

Вы, наверное, есть только 6 пунктов в вашем TableData.

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return [TableData count]; 

} 

В вашем случае «TableData» является «tripList».

0

проверить свой вид для заголовка в разделе. если вы используете массив для ваших представлений заголовков, то этот объект массива не распределяется должным образом. он использует последний объект для всего, что происходит, и заменяет старый. Создайте выделенный памятью массив массива, прежде чем добавлять к нему объекты. скажите мне, является ли это решением

+0

Да, точно, и поскольку вы повторно используете ячейки, они покажут старые ячейки, которые уже были заполнены. Вот почему я сказал, что вы должны ограничить количество ячеек точно количеством объектов, которые у вас есть в вашем массиве, и вы должны быть в порядке. –

0

Ваш код пытается повторно использовать ячейки, но так, как вы его неправильно закодировали. См. Мой ответ here для соответствующего шаблона. В оболочке ореха вы должны определять только атрибуты общих ячеек в разделе if (cell == nil), атрибуты строки, такие как текст ярлыка и т. Д., Должны быть определены за пределами этого раздела. Это не относится к вашей строке с индексом 0.

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