0
我需要为我的UITableView保存选定的项目在NSMutableDictionary为实现复选框。所以我做了以下几点:UITableView保存选定的项目
NSMutableDictionary * selDict;// - instance variable
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([selDict objectForKey:indexPath])
[selDict setObject:[NSNumber numberWithBool:FALSE] forKey:indexPath];
else
[selDict setObject:[NSNumber numberWithBool:TRUE] forKey:indexPath];
[[self tableView]reloadData];
}
- (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];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
if ([selDict objectForKey:indexPath])
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
cell.accessoryType = UITableViewCellAccessoryNone;
...
return cell;
}
但它只是设置项目检查,并没有进一步工作。
你的意思是什么不能进一步工作?你也应该更好地使用'[[self tableView] reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]'而不是'[[self tableView] reloadData]'(无需重新加载整个表) –
感谢您的提示。它也适用于我。 –