これは、私の 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];