2011-02-01 74 views
0

我在我的應用程序中使用分組表格視圖。我在表中有2個部分。第一部分有3行,第二部分有10多行。當滾動部分時,它顯示第5行第5行之後的第1行的行。我該怎麼辦。Iphone中的分組表格視圖

這裏是我的代碼...

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // Return the number of sections. 
    return 2; 
} 


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

    switch (section) { 
     case 0: 
      return 3; 
      break; 
      case 1: 
      return 8; 
       break; 

     default: 
      break; 
    } 

    return 5; 
} 


// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

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

    if (indexPath.section==0) 
    { 

     if (indexPath.row==0) 
     { 
      UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)]; 
      [Name setText:@"First Name "]; 
      [cell.contentView addSubview:Name]; 

      UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)]; 
      [email protected]"Enter First Name"; 
      [textName setBorderStyle:UITextBorderStyleNone]; 
      textName.clearButtonMode = UITextFieldViewModeWhileEditing; 
      textName.keyboardType=UIKeyboardTypeDefault; 
      textName.returnKeyType=UIReturnKeyDone; 
      textName.delegate=self; 
      [cell.contentView addSubview:textName]; 


     } 

    if (indexPath.row==1) 
    { 
     UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 110, 20)]; 
     [Name setText:@"Middle Name"]; 
     [cell.contentView addSubview:Name]; 

     UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)]; 
     [email protected]"Enter Middle Name"; 
     [textName setBorderStyle:UITextBorderStyleNone]; 
     textName.clearButtonMode = UITextFieldViewModeWhileEditing; 
     textName.keyboardType=UIKeyboardTypeDefault; 
     textName.returnKeyType=UIReturnKeyDone; 
     textName.delegate=self; 
     [cell.contentView addSubview:textName]; 


    } 

    if (indexPath.row==2) 
    { 
     UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)]; 
     [Name setText:@"Last Name "]; 
     [cell.contentView addSubview:Name]; 

     UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)]; 
     [email protected]"Enter Last Name"; 
     [textName setBorderStyle:UITextBorderStyleNone]; 
     textName.clearButtonMode = UITextFieldViewModeWhileEditing; 
     textName.keyboardType=UIKeyboardTypeDefault; 
     textName.returnKeyType=UIReturnKeyDone; 
     textName.delegate=self; 
     [cell.contentView addSubview:textName]; 


    } 
    } 


    if (indexPath.section==1) { 

     if (indexPath.row==0) { 
      [email protected]"1"; 
     } 
     if (indexPath.row==1) { 
      cell.textLabel.t[email protected]"2"; 
     } 
     if (indexPath.row==2) { 
      [email protected]"3"; 
     } 
     if (indexPath.row==3) { 
      [email protected]"4"; 
     } 
     if (indexPath.row==4) { 
      [email protected]"5"; 
     } 
     if (indexPath.row==5) { 
      [email protected]"6"; 
     } 
     if (indexPath.row==6) { 
      [email protected]"7"; 
     } 
     if (indexPath.row==7) { 
      [email protected]"8"; 
     } 


    } 


    return cell; 
} 

回答

0

您需要2個小區標識。問題是你使用相同的標識符出隊單元。 粘貼代碼,應該工作正常:D

P.S:我沒有解決您的標籤問題。每次顯示單元格時都會分配它們。那是錯的。

#pragma mark - 
#pragma mark Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // Return the number of sections. 
    return 2; 
} 


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

    NSInteger returnValue = 5; 
    switch (section) { 
     case 0: 
     { 
      returnValue = 3; 
      break; 
     } 
     case 1: 
     { 
      returnValue = 8; 
      break; 
     } 
     default: 
      break; 
    } 

    return returnValue; 
} 


// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifierSection1 = @"Cell1"; 
    static NSString *CellIdentifierSection2 = @"Cell2"; 

    UITableViewCell *cell;  
    if (indexPath.section==0) 
    { 
     cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierSection1]; 
     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierSection1] autorelease]; 
     } 



     if (indexPath.row==0) 
     { 
      UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)]; 
      [Name setText:@"First Name "]; 
      [cell.contentView addSubview:Name]; 

      UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)]; 
      [email protected]"Enter First Name"; 
      [textName setBorderStyle:UITextBorderStyleNone]; 
      textName.clearButtonMode = UITextFieldViewModeWhileEditing; 
      textName.keyboardType=UIKeyboardTypeDefault; 
      textName.returnKeyType=UIReturnKeyDone; 
      textName.delegate=self; 
      [cell.contentView addSubview:textName]; 


     } 

     if (indexPath.row==1) 
     { 
      UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 110, 20)]; 
      [Name setText:@"Middle Name"]; 
      [cell.contentView addSubview:Name]; 

      UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)]; 
      [email protected]"Enter Middle Name"; 
      [textName setBorderStyle:UITextBorderStyleNone]; 
      textName.clearButtonMode = UITextFieldViewModeWhileEditing; 
      textName.keyboardType=UIKeyboardTypeDefault; 
      textName.returnKeyType=UIReturnKeyDone; 
      textName.delegate=self; 
      [cell.contentView addSubview:textName]; 


     } 

     if (indexPath.row==2) 
     { 
      UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)]; 
      [Name setText:@"Last Name "]; 
      [cell.contentView addSubview:Name]; 

      UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)]; 
      [email protected]"Enter Last Name"; 
      [textName setBorderStyle:UITextBorderStyleNone]; 
      textName.clearButtonMode = UITextFieldViewModeWhileEditing; 
      textName.keyboardType=UIKeyboardTypeDefault; 
      textName.returnKeyType=UIReturnKeyDone; 
      textName.delegate=self; 
      [cell.contentView addSubview:textName]; 


     } 
    } 


    if (indexPath.section==1) { 
     cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierSection2]; 
     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierSection2] autorelease]; 
     } 

     if (indexPath.row==0) { 
      [email protected]"1"; 
     } 
     if (indexPath.row==1) { 
      [email protected]"2"; 
     } 
     if (indexPath.row==2) { 
      [email protected]"3"; 
     } 
     if (indexPath.row==3) { 
      [email protected]"4"; 
     } 
     if (indexPath.row==4) { 
      [email protected]"5"; 
     } 
     if (indexPath.row==5) { 
      [email protected]"6"; 
     } 
     if (indexPath.row==6) { 
      [email protected]"7"; 
     } 
     if (indexPath.row==7) { 
      [email protected]"8"; 
     } 


    } 


    return cell; 
} 

編輯:

可以使用不同的標識符對於每個類型的細胞。所以如果你有兩種類型的單元格,你可以使用兩個標識符,如果你有一個類型使用一個標識符等等。該標識符用於使特定類型的單元出隊,如果沒有單元可以重新使用,它將簡單地分配一個新單元。

+0

嘿感謝您的回覆...它的工作:) – 2011-02-01 10:02:37