2012-03-07 9 views
1

私はカスタムセルを作成しました。これらのカスタムセルをテーブルビューで使用すると、セルが置き換えられます。テーブルビューに約10個のセクションが作成され、各セクションには1行が含まれています。私はすべての10のセクションのカスタムセルを使用しています。ビューをスクロールすると、最後の4つのセルがトップセルに置き換えられます。 ReuseIdentifierを使用していますが、まだ置き換えられています。これを修正するためのアイデアは何ですか?ありがとう!カスタムセルが他のセルに置き換えられる

これは私のカスタムセルコードです!

-(UITableViewCell *)returnCellForBirthdayDetails:(UITableView *)tableView 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCellForBirthdayDetails"]; 

    if(cell == nil) 
    { 
     [[NSBundle mainBundle] loadNibNamed:@"CustomCellToAddDetails" owner:self options:nil]; 
     cell = customCell; 
     //  customCell = nil; 
    } 

    return cell; 
} 
-(BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    [customCellTextField resignFirstResponder]; 
    return YES; 
} 

- (UITextField *)textField 
{ 
    UITextField *textField = nil; 

    if(customCell) 
    { 
     textField = (UITextField *)[customCell.contentView viewWithTag:101]; 
    } 

    return textField; 
} 

とここで私は私が私が作っていますどのような間違いお知らせくださいのtableView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     UITableViewCell *cell = nil; 

    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    addCustomCell = [[CustomCellToAddDetailsController alloc] init]; 

    switch (indexPath.section) 
    { 
     case 0: 
      cell = [addCustomCell returnCellForBirthdayDetails:tableView]; 
      addCustomCell.customCellLabel.text = @"FirstName"; 
      addCustomCell.customCellLabel.font = [UIFont boldSystemFontOfSize:12]; 
      addCustomCell.customCellTextField.placeholder = @"FirstName"; 
      [delegate.fieldArray addObject:addCustomCell]; 
      break; 
     case 1: 
      cell = [addCustomCell returnCellForBirthdayDetails:tableView]; 
      addCustomCell.customCellLabel.text = @"LastName"; 
      addCustomCell.customCellLabel.font = [UIFont boldSystemFontOfSize:12]; 
      addCustomCell.customCellTextField.placeholder = @"LastName"; 
      [delegate.fieldArray addObject:addCustomCell]; 
      break; 
     case 2: 
      cell = [addCustomCell returnCellForBirthdayDetails:tableView]; 
      addCustomCell.customCellLabel.text = @"Dob"; 
      addCustomCell.customCellLabel.font = [UIFont boldSystemFontOfSize:12]; 
      addCustomCell.customCellTextField.placeholder = @"Dob"; 
      addCustomCell.customCellTextField.inputView = dp; 
      addCustomCell.customCellTextField.inputAccessoryView=toolBarForTableView; 
      [delegate.fieldArray addObject:addCustomCell]; 
      break; 
     case 3: 
      cell = [addCustomCell returnCellForBirthdayDetails:tableView]; 
      addCustomCell.customCellLabel.text = @"Address"; 
      addCustomCell.customCellLabel.font = [UIFont boldSystemFontOfSize:12]; 
      addCustomCell.customCellTextField.placeholder = @"Enter Address"; 
      [delegate.fieldArray addObject:addCustomCell]; 
      break; 
     case 4: 
      cell = [addCustomCell returnCellForBirthdayDetails:tableView]; 
      addCustomCell.customCellLabel.text = @"City"; 
      addCustomCell.customCellLabel.font = [UIFont boldSystemFontOfSize:12]; 
      addCustomCell.customCellTextField.placeholder = @"Enter City"; 
      [delegate.fieldArray addObject:addCustomCell]; 
      break; 
     case 5: 
      cell = [addCustomCell returnCellForBirthdayDetails:tableView]; 
      addCustomCell.customCellLabel.text = @"State";  
      addCustomCell.customCellLabel.font = [UIFont boldSystemFontOfSize:12]; 
      addCustomCell.customCellTextField.placeholder = @"Enter State"; 
      [delegate.fieldArray addObject:addCustomCell]; 
      break; 
     case 6: 
      cell = [addCustomCell returnCellForBirthdayDetails:tableView]; 
      addCustomCell.customCellLabel.text = @"Email"; 
      addCustomCell.customCellLabel.font = [UIFont boldSystemFontOfSize:12]; 
      addCustomCell.customCellTextField.placeholder = @"Enter Email"; 
      [delegate.fieldArray addObject:addCustomCell]; 
      break; 
     case 7: 
      cell = [addCustomCell returnCellForBirthdayDetails:tableView]; 
      addCustomCell.customCellLabel.text = @"Phone"; 
      addCustomCell.customCellLabel.font = [UIFont boldSystemFontOfSize:12]; 
      addCustomCell.customCellTextField.placeholder = @"Enter MobileNo"; 
      [delegate.fieldArray addObject:addCustomCell]; 
      break; 
    } 
    return cell; 
    [addCustomCell release]; 

} 

でカスタムセルを呼び出しています。ありがとう!!

+0

コードを入力してください。 – Shubhank

+0

@kiran質問にいくつかのコードを追加しようとすると、それは良いだろう.. !! – Kamarshad

+1

確かに私はコードを追加しています... – Kiran

答えて

1

セルごとに異なる再使用識別子を使用します。同じ識別子を使用すると、tableviewはこの識別子を持つ使用可能なセルをすべて取得します(使用していないので上のセルを取得しています)。

編集: text/imageを除くすべてのセルで同じ識別子を使用することができます。次に、各セルにtext/image/etcを設定する 'configureCell'メソッドを呼び出すことができます。

+0

:ここで私は物語のビューのすべてのセクションに1つのカスタムセルを使用しています。それでは、どのようにそれぞれ異なる識別子を使用できますか?10個のカスタムセルを作成する必要がありますか? – Kiran

+0

10個のカスタムセルを作成するか、適切な情報でセルを構成するtableViewデリゲートの ' - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath'メソッドを実装する必要があります。それが呼び出されるたびに。 –

2

セルが再利用されているため、交換されています。

  1. 毎回新しいセルを作成する必要があります(dequeueReusableIdentifierを使用しないでください)。または、各セルに異なる識別子を使用してください。非効率です。
  2. セルを描画する前に必ずそのセルをクリーンアップしてください。 (hereをご覧ください)
関連する問題