2012-03-07 136 views
1

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

这是我的CustomCell代码!在的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]; 

} 

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

,并在这里,我呼吁定制单元请让我知道我在做什么错误。谢谢!!

+0

给你的代码。 – Shubhank 2012-03-07 08:43:29

+0

@kiran尝试附加一些代码的问题,它会很好..! – Kamarshad 2012-03-07 08:43:58

+1

当然,我正在添加代码... – Kiran 2012-03-07 08:45:42

回答

1

使用不同reuseIdentifier每个细胞。当你使用相同的标识符时,tableview将只抓住任何可用的单元格(在你的情况下,由于它们不再被使用,所以它抓住了顶部的单元格)。

编辑: 您可以使用相同的标识符(你应该),对于那些除了文字/图片都相同的细胞。然后你可以调用一个'configureCell'方法,为每个单元设置文本/图像/ etc。

+0

:在这里,我使用一个自定义单元格为故事视图中的所有部分。那我该如何使用不同的标识符呢?你的意思是我需要创建10个自定义单元吗? – Kiran 2012-03-07 09:04:51

+0

你要么需要创建10层自定义的细胞,或者你需要在的tableView委托的'来实现功能 - (UITableViewCell的*)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath'方法配置用正确的信息的单元格每次调用它。 – 2012-03-07 22:57:03

2

是越来越替换,因为你的细胞被重用。

  1. 要么你必须创建新的细胞每次。(不要使用dequeueReusableIdentifier,或者使用不同的标识符为每个单元格。这是一种低效)。
  2. 每次绘制之前清理单元格。 (看here
相关问题