2012-05-04 58 views
0

我正在開發一個通用應用程序。在這個應用程序中有一個UITableView。問題在於,TableView中的標題行在iphone中的6行和iPad中的9行之後重複。Iphone:UITableView標題行在6行後重復

我GOOGLE了,但沒有找到解決方案來解決這個頭問題。請幫忙。

- (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

把viewForHeader的代碼放在 – Saad

回答

1

你行的情況下,我得到了,我刪除

if (cell == nil) 

它工作得很好。實際上,它正在第一個標題行中查找它的標籤,並且無法替換它的值,所以造成了問題。

+0

我也這樣做,但它會崩潰我的應用程序 –

1

你可能在你的資料表只有6項。

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

    return [TableData count]; 

} 

您的情況'TableData'是'tripList'。

0

請在部分檢查您的viewFor標頭。如果你使用數組作爲你的頭部視圖,那麼這個數組對象沒有正確的分配內存。它使用最後一個對象來代替所有的舊對象。在添加對象之前,先創建一個內存分配的數組對象。告訴我,如果這是解決方案

+0

的部分和編號中,是的,因爲您正在重複使用單元格,它們會向您顯示已經填充的舊單元格。這就是爲什麼我說你應該限制細胞的數量到你陣列中的對象的數量,你應該沒問題。 –

0

你的代碼試圖重用單元格,但你編碼的方式是錯誤的。有關適當的模式,請參閱我的回答here。在堅果殼中,您應該只在if (cell == nil)部分定義共享單元格屬性,行標籤文本等特定屬性應在此部分之外定義。這不是在索引0