UITableView save selected items
I need to save selected items for my UITableView in the NSMutableDictionary for iplementing checkboxes. So I do the following:
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:U开发者_C百科ITableViewCellSelectionStyleNone];
}
if ([selDict objectForKey:indexPath])
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
cell.accessoryType = UITableViewCellAccessoryNone;
...
return cell;
}
But it is only sets the items checked and does't work further.
You might want to change
if ([selDict objectForKey:indexPath])
to
if ([[selDict objectForKey:indexPath] boolValue])
What you are doing now is checking whether the object exists or not. It will work once when it is nil
but not after that.
精彩评论