2011-02-01 12 views
0

私のアプリでグループ化テーブルビューを使用しています。私はテーブルに2つのセクションを持っています。最初のセクションは3行、2行目は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) { 
      [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; 
} 

編集:

あなたは、細胞の種類ごとに異なる識別子を使用することができます。したがって、2つのタイプのセルを使用する場合は、1つのタイプを持つ場合は2つの識別子を使用し、1つのタイプを使用するなどです。識別子は、特定のタイプのセルをデキューするために使用されます。再利用可能なセルがない場合は、単に新しいセルを割り当てます。

+0

お返事ありがとうございました...その作業:) –

+0

@Alex私はこれをよりよく理解したいと思います。なぜこの状況は2つの識別子を必要としますか?各セクションには常に識別子が必要ですか?ありがとう。 –

+0

私の編集を見てください。 –

関連する問題