2012-04-18 151 views
0

我一直在試圖弄清楚這一點。我在它自己的xib文件中創建一個自定義單元格。在我的視圖控制器中,我已經設置了一個表格視圖控制器。正在被拖入表格視圖的數據是基於一個來自我擁有的核心數據的獲取請求控制器。我在cellForRowAtIndexPath函數中設置了自定義單元格。我爲這個函數中的每個單元格創建一個標籤,並用來自管理對象的一些數據填充標籤。當我第一次運行時,一切似乎都好。但是,當我嘗試上下滾動並重新使用新單元格時,標籤中的數據將放置在錯誤的單元格中。我已經看到和聽到這與細胞的再利用有關。但是,在糾正這個問題上還沒有看到太多例子。下面是我在我的cellForRowAtIndexPath函數中的一些代碼。讓我知道是否可能需要其他輸入。謝謝你的幫助。IOS自定義單元格,當單元格重用時顯示錯誤文本的IOS自定義單元格

-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:@"CustomCell"]; 

    /* do this to get unique value per cell due to sections. */ 
    NSInteger indexForCell = indexPath.section * 1000 + indexPath.row + 1; 

    NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath]; 



    NSString *lastSession = nil; 
    UILabel *lastSessionLabel = nil; 
    if(cell == nil) { 

     lastSession = [managedObject valueForKey:@"last_session"]; 

     [self.tableView registerNib:[UINib nibWithNibName:@"CustomCell" 
                bundle:[NSBundle mainBundle]] 
      forCellReuseIdentifier:@"CustomCell"]; 

     self.tableView.backgroundColor = [UIColor clearColor]; 
     cell = [aTableView dequeueReusableCellWithIdentifier:@"CustomCell"]; 

     lastSessionLabel = [[UILabel alloc]initWithFrame:CGRectMake(410,55, 89, 35)]; 
     lastSessionLabel.textAlignment = UITextAlignmentLeft; 
     lastSessionLabel.tag = indexForCell; 
     lastSessionLabel.font = [UIFont systemFontOfSize:17]; 
     lastSessionLabel.highlighted = NO; 
     lastSessionLabel.backgroundColor = [UIColor clearColor]; 

     cell.contentView.tag = indexForCell; 
     [cell.contentView addSubview:lastSessionLabel]; 
    } else { 
     lastSessionLabel = (UILabel *)[cell viewWithTag:indexForCell]; 
    } 

     if (lastSession && lastSession.length) {   
      lastSessionLabel.text = lastSession; 
     } 


    cell.textLabel.text = [NSString stringWithFormat:@"%@%@%@%@", @"Dr. ", 
          [managedObject valueForKey:@"first_name"], 
          @" " , 
          [managedObject valueForKey:@"last_name"]]; 

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    cell.editingAccessoryType = UITableViewCellAccessoryNone; 


    return cell; 
} 

**修改過的代碼**

下面是修改代碼:在viewDidLoad中如下:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self.tableView registerNib:[UINib nibWithNibName:@"CustomCell" 
              bundle:[NSBundle mainBundle]] 
     forCellReuseIdentifier:@"CustomCell"]; 

     self.tableView.backgroundColor = [UIColor clearColor]; 
    }  

in -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath: (NSIndexPath *)indexPath { 
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:@"CustomCell"]; 
    NSInteger indexForCell = indexPath.section * 1000 + indexPath.row + 1; 
    NSLog(@"index for cell: %d",indexForCell); 

    NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath]; 


    NSString *lastSession = [managedObject valueForKey:@"last_session"]; 
UILabel *lastSessionLabel = nil; 
    if(cell == nil) { 
     NSLog(@"Cell is nil! %@", [managedObject valueForKey:@"first_name"]); 

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomCell"]; 


     self.tableView.backgroundColor = [UIColor clearColor]; 
    }  
     lastSessionLabel = [[UILabel alloc]initWithFrame:CGRectMake(410,55, 89, 35)]; 
     lastSessionLabel.textAlignment = UITextAlignmentLeft; 
     lastSessionLabel.tag = indexForCell; 
     lastSessionLabel.font = [UIFont systemFontOfSize:17]; 
     lastSessionLabel.highlighted = NO; 
     lastSessionLabel.backgroundColor = [UIColor clearColor]; 


     [cell.contentView addSubview:lastSessionLabel]; 
     /* Appropriate verbiage for nil last session. */ 
     if (lastSession && lastSession.length) {   
      lastSessionLabel.text = lastSession; 
     } 
return cell; 
} 

I am still having issues again with the label cell text changing when I scroll for different cells. I read some where about maybe having to use the prepareForReuse function for this. 

回答

1

你只取lastSession當你創建一個新的細胞。請嘗試在if(cell == nil)聲明之前放置此行。

lastSession = [managedObject valueForKey:@"last_session"]; 

I.e.這樣的:

NSString *lastSession = [managedObject valueForKey:@"last_session"]; 

在此代替:

NSString *lastSession = nil; 

UPDATE

您還設置了相同的標籤爲兩種觀點:基於您的代碼

lastSessionLabel.tag = indexForCell; 
... 
cell.contentView.tag = indexForCell; 

您只應使用第一行的樣本,即爲設置標籤

第二次更新

你應該也只能在你看來生命週期調用registerNib:一次,例如在viewDidLoad,不是每次你需要一個新的單元格。此外,如果使用cell == nil而不是使用dequeueReusableCellWithIdentifier:,則應該創建一個新單元格。例如。

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomCell"]; 
+0

感謝您的及時回覆。我試過想要你提到,但我仍然遇到隨着標籤文本隨機變化的問題,當我上下滾動查看不同的單元格。 – doubleace3 2012-04-19 00:59:43

+0

請參閱我的更新 – 2012-04-19 10:06:36

+0

相同的問題。似乎滾動標籤中的內容發生變化並且不一致。也許不同的方法是必要的。 – doubleace3 2012-04-19 16:04:54