包括的な選択リストとして動作するようにUITableViewをプログラミングしています。私のテーブルは正しく表示され、複数のセルをチェックボックスで選択することができます。私の問題は、選択されたセル(右にチェックマークが付いているセル)は、スクロールして表示されていないとき(セルのチェックマークが消える)、選択したステータスが失われてしまうことです。私は、セルが表示外にスクロールされても、テーブル内のセルに選択されたものを保存しておきたい。これを引き起こしていることは誰にも分かりますか?ここでUITableViewを包括的な選択リストとして構成する際の問題
は私のTableViewControllerクラスの内部に私のコードです:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [widgetTitles_glob objectAtIndex:row];
cell.detailTextLabel.text = @"";
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
// Reflect selection in data model
} else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
// Reflect deselection in data model
}
}
すべてのヘルプは非常に高く評価されるだろう。
if(cell == nil)条件は、割り当てられたテーブル内のすべてのセルを保持するべきではありませんか? – Sabobin
indexPath.rowを格納するだけでなく、indexPath全体を格納する必要があります。 'if([selectedRows containsObject:indexPath.row])をチェックすると、cell.accessoryType = UITableViewCellAccessoryCheckmark; else セル。accessoryType = UITableViewCellAccessoryNone; 'スクロールすると、各セクションの最初の行がすべてチェックマークが付けられます。代わりに、セクションと行の両方を保持するため、indexPath全体を保存する必要があります。 – tazboy