5

これは、私の cellForRowAtIndexPath UITableView デリゲート メソッドの要約コードです。

- (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 個のセルしかない) に、最初のセクションのセル 0 にのみ割り当てられるはずの情報が割り当てられていることです。したがって、deleteButton の背景などを取得する代わりに、ラベルのタイトル「foobar」を取得します。私のifステートメントはかなり明確なので、なぜこれが起こっているのかよくわかりません。何か案は?

編集: backgroundView を NULL に設定すると、テキストを含むセルがビューを離れるときに、背景なしで戻ってきます。したがって、それは実行可能な解決策ではありません。また、detailTextLabel のテキストは、テキストを持つべきでないセルに設定されたままです。

これは、セルの backgroundViews が nil に設定され、テキストが表示されるべきではない削除セルに表示されている場合の外観です。

ここに画像の説明を入力

Alex Deem が推奨する解決策 (古いデキュー コードをこのコードに置き換える):

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];
4

3 に答える 3

5

セルの再利用に関するドキュメントを読む必要があります。

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

于 2011-12-02T03:50:10.723 に答える
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];
}
cell.textLabel.text=@"unique text";
return cell;
于 2011-12-02T03:15:21.527 に答える