自定义单元格被其他单元格替换

时间:2012-03-07 08:40:34

标签: iphone ios uitableview

我创建了一个自定义单元格。当我在表视图中使用这些自定义单元格时,我的单元格被替换。我在表格视图中创建了大约10个部分,其中每个部分包含1行。我正在为所有10个部分使用自定义单元格。当我滚动视图时,最后4个单元格被顶部单元格替换。我正在使用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];

}

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

2 个答案:

答案 0 :(得分:2)

它正在被替换,因为您的细胞正在被重复使用。

  1. 每次都必须创建新的单元格。(不要使用dequeueReusableIdentifier。或者为每个单元格使用不同的标识符。效率低下。)
  2. 每次绘制前都要清理单元格。 (看here

答案 1 :(得分:1)

为每个单元格使用不同的reuseIdentifier。当您使用相同的标识符时,tableview将只使用此标识符获取任何可用的单元格(在您的情况下,它会抓取顶部的标识符,因为它们不再使用)。

编辑: 对于除text / image之外的所有内容都相同的单元格,您可以使用相同的标识符(并且应该)。然后,您可以调用'configureCell'方法,为每个单元格设置text / image /。