2011-12-02 5 views
5

これは私のcellForRowAtIndexPathのUITableViewデリゲートメソッドの要約コードである:のUITableView indexPath部ロジック

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"blahblahblah"]; 
if (cell == nil) { 
    // No cell to reuse => create a new one 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"blahblahblah"] autorelease]; 
} 
cell.textLabel.text = @""; 
cell.detailTextLabel.text = @""; 
cell.backgroundView = NULL; //problem here 

// Initialize cell 
//blah blah blah 
//now to the good part... 

if(indexPath.section == 1) { 
    cell.backgroundView = deleteButton; 
    cell.userInteractionEnabled = YES; 
    cell.textLabel.text = nil; 
    cell.detailTextLabel.text = nil; 
} 

else if(indexPath.section == 0) { 
    NSLog(@"section: %i row: %i", indexPath.section, indexPath.row); 
    switch (indexPath.row) { 
     case 0: 
      cell.textLabel.text = @"foobar"; 
      //more stuff 
      break; 

     //lots more cases 

     default: 
      break; 
    } 
} 

return cell; 

}

私の問題は、セクション1(セクション0の最初のセルは、10個の細胞、セクション1を有することです1つのセルのみを有する)は、第1セクションのセル0に割り当てられると想定される情報が割り当てられる。そのため、deleteButtonのバックグラウンドなどを取得する代わりに、ラベルのタイトル「foobar」を取得します。 if文がかなり明確であるため、なぜこれが起こっているのか分かりません。何か案は?

編集:backgroundViewをNULLに設定すると、テキスト付きのセルは、ビューを離れるときにバックグラウンドなしに戻ります。それは実行可能な解決策ではありません。また、detailTextLabelのテキストは、テキストを持たないセルに設定されています。

これは、それがnilに設定されたセルのbackgroundViewsとどこがいけない、削除、セル上に表示テキストを、どのように見えるかです:

enter image description here

ソリューション、アレックスと考える(で推奨されているように)このコードの古いデキューコードを置き換える:

NSString* identifier; 
if(indexPath.section == 0) 
    identifier = @"0"; 
else 
    identifier = @"1"; 
UISwitch *switchView; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 
if (cell == nil) { 
    // No cell to reuse => create a new one 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier] autorelease]; 

答えて

5

の順に行う必要があります。セルの再利用に関するドキュメントをお読みください。

基本的に異なるスタイルのセルであるため、2つのセクションそれぞれに異なるreuseIdentifierを使用する必要があります。

+0

ああ、そうだった。問題をソリューションで更新しました。 –

0

これらのコード行に閉じ}ブラケットを追加します。

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"blahblahblah"]; 
if (cell == nil) { 
    // No cell to reuse => create a new one 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"blahblahblah"] autorelease]; 
} 
.... 
.... 
.... 
if(indexPath.section == 1) { 
    .... 
    .... 
} 
if(indexPath.section == 0) { 
    .... 
    .... 
} 

あなたはあなたのテーブルに良い結果があると思う。

今のところ、(少なくとも私があなたのコードで伝えることができる限り)何かに初期化されたセルが1つ作成されます。要求されている他のデータの値&にリセットされることはありません。

+0

それはしませんでした - セクションの行0の両方のセルは、セクション0のセルのテキストとセクション1のセルの背景を取得します。しかし、私はそれを発見したdequeueReusableCellWithIndentifierとreuseIndentifierに異なる識別子を使用しています...これは悪い習慣ですか? –

+0

あなたの新しいコードで元の質問を更新してください。そして、はい、識別子は単一のテーブルビュー内で同じでなければなりません。 –

+0

私は新しいコード –

0

コードが正しく構成されていません。それは使用されなくなった場合

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"blahblahblah"]; 
if (cell == nil) 

dqueueReuseableCellWithIdentifierは、既存の以前に作成したセルを返しません。 If cell == nil新しいセルを作成し、すべてのセルに共通のデフォルトを設定する必要があります。ただし、そのindexPathに固有のデータの設定は、

if(cell==nil) block. 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"blahblahblah"]; 
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1   reuseIdentifier:@"blahblahblah"] autorelease]; 
} 
[email protected]"unique text"; 
return cell; 
+0

Micheal Dautermannの答えに投稿したコメントをご覧ください。 –

関連する問題