2012-03-07 139 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
相關問題