これは私の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とどこがいけない、削除、セル上に表示テキストを、どのように見えるかです:
ソリューション、アレックスと考える(で推奨されているように)このコードの古いデキューコードを置き換える:
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];
ああ、そうだった。問題をソリューションで更新しました。 –