自定义单元格被其他单元格replace

我创build了一个自定义单元格 当我在表格视图中使用这些自定义单元格时,我的单元格被replace了。我在表格视图中创build了大约10个部分,其中每个部分包含1行。 我为所有10个部分使用自定义单元格。 当我滚动视图时,最后4个单元格被replace为顶部单元格。 我正在使用ReuseIdentifier,但他们仍然被取代。 任何想法来解决这个问题? 谢谢!

这是我的CustomCell代码!

-(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]; } 

请让我知道我正在犯什么错误。 谢谢!!

为每个单元格使用不同的reuseIdentifier。 当你使用相同的标识符,那么tableview将只抓住任何可用的单元格与这个标识符(在你的情况下,抓住顶部的,因为他们不再使用)。

编辑:你可以使用相同的标识符(你应该)为除了文本/图像的一切都相同的单元格。 然后你可以调用一个'configureCell'方法,在这里为每个单元格设置text / image / etc。

它正在被取代,因为你的细胞正在被重新使用。

  1. 或者你必须每次创build新的单元格(不要使用dequeueReusableIdentifier,或者为每个单元格使用不同的标识符,效率不高)。
  2. 在绘制之前,每次清理单元格。 (看这里 )