2012-04-12 8 views
2

この質問は一意ではないと確信していますが、解決するための他の情報源は見つかりませんでした。実際、それらはより混乱を招いています。できれば光を捨ててください。再利用可能なセルとの混同(不正なセルがスクロールの後に影響を受ける)

問題の要点は、カスタムセルを持つ7セクションの30行テーブルの1つのセルにUILabelをスライド(-50)したいということです。いくつかの行をスクロールすると、他の行がこのラベルを移動しており、スクロールが続行されると、さらに遠くに移動します。

マイナス50 x座標はテストのために2つの場所にあることに注意してください。セル== nilの最初のものは決して呼び出されません。なぜ??私はそれがこの問題を解決する場所だと思った。

また、セルがnilであっても、initWithStyle(カスタム用ではありません)またはinitWithCoder(ストーリーボードで作成された)の呼び出し方法は不明です。 ??

NSArray *a = [[userDetailData objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]]; 

    NSString *key = [[[a objectAtIndex:0] allKeys] objectAtIndex:0]; 
    NSString *value = [[a objectAtIndex:0] valueForKey:key]; 

    static NSString *CellIdentifier = @"MasterCell"; 

    UsersTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    // If cell does not exist, create it, otherwise customize existing cell for this row 
    if (cell == nil) { 
     // Create cell 
     cell = [[UsersTableViewCell alloc] initWithCoder:<#(NSCoder *)#>reuseIdentifier:CellIdentifier]; 

     // Configure cell: 
     NSLog(@"Key: %@ %d, %d", key, [indexPath section], [indexPath row]); 
     // Prepare the date field to move left for checkmark or ex-mark 

     if ([key isEqualToString:@"Date"]) { 
      CGRect frame = cell.comboLabel.frame; 
      frame.origin.x -= 50; 
      cell.comboLabel.frame = frame; 
      NSLog(@"In Date... minus 50"); 
     } 
    } 

    // Customize cell: 
    CGRect frame = cell.comboLabel.frame; 
    if ([key isEqualToString:@"Date"]) { 
     frame.origin.x -= 50; 
     cell.comboLabel.frame = frame; 
     NSLog(@" %d, %d\n cell: %@\n", [indexPath section], [indexPath row], cell); 
    } 
    else { 
     frame.origin.x += 0; 
     cell.comboLabel.frame = frame; 
    } 

    // Reset color 
    cell.comboLabel.textColor = [UIColor blackColor]; 

    // Set the Item 
    cell.nameLabel.text = [NSString stringWithFormat: @" %@",key]; 
    // Give the cell an accessory indicator 
    ..... continue on with logic. 

答えて

1

if文の直前で再利用可能なセルに割り当てたので、cellはnilではありません。

私は「日付セル」などの識別子で二プロトタイプセルにすることを検討して作成しているセルがキー「日」ここ

を持っている場合は、再利用可能なセルの第2のセットを作ります:

NSArray *a = [[userDetailData objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]]; 

NSString *key = [[[a objectAtIndex:0] allKeys] objectAtIndex:0]; 
NSString *value = [[a objectAtIndex:0] valueForKey:key]; 

static NSString *CellIdentifier = @"MasterCell"; 
static NSString *DateCellIdentifier = @"Date Cell"; 

UsersTableViewCell *cell; 

//Customize Cell 

if ([key isEqualToString:@"Date"]) { 

    cell = [tableView dequeueReusableCellWithIdentifier:DateCellIdentifier]; 
    CGRect frame = cell.comboLabel.frame; 

    frame.origin.x -= 50; 
    cell.comboLabel.frame = frame; 
    NSLog(@" %d, %d\n cell: %@\n", [indexPath section], [indexPath row], cell); 
} 
else { 
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    frame.origin.x += 0; 
    cell.comboLabel.frame = frame; 
} 

// Reset color 
cell.comboLabel.textColor = [UIColor blackColor]; 

// Set the Item 
cell.nameLabel.text = [NSString stringWithFormat: @" %@",key]; 
// Give the cell an accessory indicator 
..... continue on with logic. 

実際、ストーリーボードでプロトタイプセルをカスタマイズするだけでは、セルのカスタマイズをほとんど行う必要はありません。

+0

OK。私はあなたが提示したアイデアを見る。それで、私たちはストーリーボードに複数の「プロトタイプのセル」を持つことができますか? – David

+0

はい、それは私が提示したいデータのいくつかの種類のテーブルを持っているときに私がそれらを使用するものです。私はちょうどデータの種類ごとに異なるプロトタイプを作成し、それに応じてcellForRowAtIndexPath関数でソートします。 –

+0

素晴らしい。それは動作します。 UILabelsを漂わせることはもうありません。ありがとうございました! – David

関連する問題