2011-09-11 15 views
1

私は2つの異なるタイプのUITableViewCellsを持つUITableViewを印刷しようとしています。UITableViewの奇妙な振る舞い

  1. 1(第3及びその後の細胞)から始まるもの(第2セル)を含まない空のテーブルビュー細胞、ちょうど黒の背景標識を含む
  2. テーブルビュー細胞、画像、等

私のテーブルビューの高さは、誰でも少なくとも3つのテーブルビューセルに収まることができます。

最初にテーブルビューを読み込むと、テーブルビューは完全にうまく見えます。最初の2つの行は黒で、3番目の行には「1」というラベルが付いています。

ただし、スクロールして下にスクロールしてから、上にスクロールしてください。私の最初の2つの空のセル(空であると思われる)は、3番目以降のセルでのみ見つかるはずのもので埋められます。

私はこの動作がセルを再利用するテーブルビューから来ていると思っていますが、それでもわかりません。

コードスニペット:セルのそれぞれ異なるタイプの場合

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    }  

    switch ([indexPath row]) { 
     case 0: 
      cell.contentView.backgroundColor = [UIColor blackColor]; 
      [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
      break; 
     case 1: 
      cell.contentView.backgroundColor = [UIColor blackColor]; 
      [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
      break; 

     default: 
      cell.contentView.backgroundColor = [UIColor blackColor]; 
      [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 

      UILabel *rowLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 30, 200, 30)]; 
      rowLabel.text = [NSString stringWithFormat:@"Row %d", [indexPath row]-1]; 
      rowLabel.backgroundColor = [UIColor blackColor]; 
      rowLabel.textColor = [UIColor whiteColor]; 
      [rowLabel setFont:[UIFont fontWithName:@"GillSans-Bold" size:18]]; 
      [cell.contentView rowLabel];     [rowLabel release]; 

      UIButton *aButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 50, 40, 40)]; 
      [aButton setImage:anImage forState:UIControlStateNormal]; 
      [cell.contentView addSubview:aButton]; 
      [aButton release]; 

      break; 
    } 

    return cell; 
} 
+0

解決策を見つけた可能性があります。私はユニークな再利用識別子の使用を提案しました。私はそれぞれの細胞ごとにそれをしました。また、スイッチケースをifに移動しました。私はstackoverflowが私にできるとき私の答えを投稿します! – tommi

+0

これは正しい解決策ではありません。可能であれば、セルを再利用する必要があります。そうしないと、スクロールが非常に遅くなる可能性があります。 – jrturton

+0

@jrturton、私に知らせてくれてありがとう!あなたが再利用を意味するとき、セル内のすべてのもの(ボタン、画像を含む)を再利用することを意味しますか? – tommi

答えて

1

デキューされたセルには、追加したすべてのボタンとラベルが含まれます。セルのデキュー時に背景色を設定するだけです。

あなたは、いくつかのオプションがあります。

  • セクション
  • が行0のセルごとに異なる再利用識別子を使用し、1
  • でグループ化されたテーブルビューを使用して、あなたの黒い部分
  • ためtableHeaderViewを使用します後者のオプションについては

、あなたのcellForRowAtIndexPathは次のようになります。

static NSString *CellIdentifier = @"Cell"; 
static NSString *blankCellIndentifier = @"BlankCell"; 

if (indexPath.row == 0 || indexPath.row == 1) 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:blankCellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:blankCellIdentifier] autorelease]; 
     cell.contentView.backgroundColor = [UIColor blackColor]; 
     [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
    } 
return cell; 
} 
else 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    UILabel *rowLabel; 
    UIButton *aButton; 

    if (cell == nil) 
    { 
     // Here, you create all of the new objects 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
     cell.contentView.backgroundColor = [UIColor blackColor]; 
     [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
     rowLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 30, 200, 30)]; 
     rowLabel.backgroundColor = [UIColor blackColor]; 
     rowLabel.textColor = [UIColor whiteColor]; 
     [rowLabel setFont:[UIFont fontWithName:@"GillSans-Bold" size:18]]; 
     [cell.contentView addSubview:rowLabel]; 
     rowLabel.tag = 1; 
     [rowLabel release]; 

     aButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 50, 40, 40)]; 
     [aButton setImage:anImage forState:UIControlStateNormal]; 
     [cell.contentView addSubview:aButton]; 
     aButton.tag = 2; 
     [aButton release]; 

    } 
    // Here, you just configure the objects as appropriate for the row 
    rowLabel = (UILabel*)[cell.contentView viewWithTag:1]; 
    rowLabel.text = [NSString stringWithFormat:@"Row %d", [indexPath row]-1]; 

    return cell; 
} 
+0

行0と1のセルに異なる再利用識別子を使用しようとしています。例: 'CellBlank'と 'Cell'?最初の2行分のCellBlank 3行目以降のセル? – tommi

+0

それはうまくいく、あなたはそれに助けが必要ですか? – jrturton

+0

ありがとうございます。私はちょうどこれらのコードを実装し、私のアプリはこれまでのところうまく動作しています! – tommi

0

、あなたは別の再利用識別子を必要としています。したがって、3つのreuseIdentifierが必要です。

+0

こんにちは@ベンジャミンメイヨー、なぜ3と2ではない?私は2つの異なる種類の細胞を持っているので、 – tommi

+0

上記のコードに3つのセルがあります。行がインデックス0、インデックス1、および他の行にあるとき。それは3種類の細胞です。 –

+0

私は上記の答えを持っています。助けてくれてありがとう。 – tommi