2011-07-05 18 views
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; 
    } 

但它只是设置项目检查,并没有进一步工作。

+0

你的意思是什么不能进一步工作?你也应该更好地使用'[[self tableView] reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]'而不是'[[self tableView] reloadData]'(无需重新加载整个表) –

+0

感谢您的提示。它也适用于我。 –

回答

2

您可能需要更改

if ([selDict objectForKey:indexPath]) 

if ([[selDict objectForKey:indexPath] boolValue]) 

你现在正在做的是检查对象是否存在。它会在nil之后工作一次,但之后不会。