2012-04-18 10 views
0

私は少しこれを理解しようとしています。自分のxibファイルにカスタムセルを作成します。私のビューコントローラでは、セクションを持つテーブルビューコントローラをセットアップしました。テーブルビューにプルされているデータは、私が持っているいくつかのコアデータからのフェッチ要求コントローラに基づいています。私はcellForRowAtIndexPath関数でカスタムセルを設定しました。この関数内の各セルのラベルを作成し、そのラベルに管理対象オブジェクトのデータを設定しています。私が最初に走った時、すべてが大丈夫と思われます。しかし、上下にスクロールして新しいセルを再利用しようとすると、ラベル内のデータが間違ったセルに配置されます。私はこれが細胞の再利用と関係しているのを見て聞いたことがあります。しかし、この問題の修正に関する多くの例は見ていない。以下は、私のcellForRowAtIndexPath関数にあるコードの一部です。他の入力が必要な場合は教えてください。助けてくれてありがとう。セルが再利用されたときにラベルが表示されている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; 
} 

は**改訂コードは**

は、以下のコードを変更している:あなたは新しいセルを作成するときにのみlastSessionを取得している

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

:のviewDidLoadで次のようです。 if(cell == nil)ステートメントの前にこの行を入れてみてください。

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

e.e.e.この:

これの代わりに
NSString *lastSession = [managedObject valueForKey:@"last_session"]; 

:あなたは、2つのビューに同じタグを設定している

NSString *lastSession = nil; 

UPDATE:

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

あなたのコードに基づいて、最初の行だけを使用してください。すなわち、のタグを設定してください

SECOND UPDATE

また唯一例えば、ビューのライフサイクルに一度registerNib:を呼び出す必要がありますviewDidLoadで、新しいセルが必要なたびにではありません。さらに、dequeueReusableCellWithIdentifier:の代わりにcell == nilの場合は、新しいセルを作成する必要があります。例えば。

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

お返事ありがとうございます。私はあなたが言及したいと思ったが、別のセルを表示するために上下にスクロールすると、ランダムに変化するラベルテキストの問題がまだ残っている。 – doubleace3

+0

私の更新 –

+0

同じ問題を参照してください。ラベル内のコンテンツをスクロールすると一貫性がないと思われます。たぶん異なるアプローチが必要かもしれません。 – doubleace3

関連する問題