私は、テーブルから1つの項目を選択するセクション化されたtableViewを持っています。彼らがアイテムを選択すると、アイテムの横にチェックが表示されます(UITableViewCellAccessoryCheckmark
を使用)。以前に選択した場合は、前に選択した行からチェックを削除する必要があります。セクション化されたテーブル内の行のaccessoryTypeを変更するユーザーが行を選択した後に表示
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int newRow = [indexPath row];
int oldRow = [lastIndexPath row];
if (newRow != oldRow || newRow == 0)
{
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
[lastIndexPath release];
lastIndexPath = indexPath;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
lastIndexPath
.h
ファイルで私的に宣言されています。ここで私が使用していたコードがあります。
このコードは、セクション化されていない小さなリストに最適です。しかし、セクション化された大きなテーブルでは、他のセクションの行にランダムなチェックマークが付けられます。ほとんどの場合、cellForRowAtIndexPath
がindexPathのセクションを無視しているようです。
最小のセクションの行数よりも大きい行を選択すると、コードがクラッシュします。ここで
はcellForRowAtIndexPathのコードです:あなたがここに持っている
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [keys objectAtIndex:section];
NSArray *itemSection = [items objectForKey:key];
static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SectionsTableIdentifier] autorelease];
}
NSArray *rowLabel = [itemSection objectAtIndex:row];
cell.textLabel.text = [rowLabel objectAtIndex:1];
NSString *detText = [rowLabel objectAtIndex:0];
detText = [detText stringByAppendingString:@" $"];
detText = [detText stringByAppendingString:[rowLabel objectAtIndex:2]];
cell.detailTextLabel.text = detText;
return cell;
}
は、 'cellForRowAtIndexPath:'のコードを参照する必要があります。私は、あなたがデキューしているセルと再利用識別子( 'cellForRowAtIndexPath:'のデフォルト実装)を使用していると仮定しています。その場合、セルを作成するためのコードは、マークされたセルをテンプレートとして再利用して新しいセルを作成します。この問題を解決するには、いくつかのことができます: "cellForRowAtIndexPath:'で毎回新しいセルを作成するか、 ''各セルへの参照を保持し、 '' cellForRowAtIndexPath: ''の呼び出しを回避する。 – ColdLogic
はい - これはランダムなチェックマークの最初の問題を解決することができますが、最小のセクションの行数よりも大きい行を選択すると、クラッシュの第2の問題が解決されるとは思われません。ここにcellForRowAtIndexPathコード: –
あなたはコメントにそのコードを投稿することはできません。元の投稿に追加する – ColdLogic