2011-12-19 70 views
0

到目前为止搜索堆栈溢出我还没有发现像我的情况。任何帮助都非常感谢:我一直看到,如果我在A人身上勾上勾号,H人也会有一个人,并且约有10人离开。基本上每10个重复一个复选标记。带复选标记的UITableViewCell,复制复选标记

这里是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

{static NSString *CellIdentifier = @"MyCell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 

      } 

cell.textLabel.text = 

[NSString stringWithFormat:@"%@ %@", [[myArrayOfAddressBooks objectAtIndex:indexPath.row] objectForKey:@"FirstName"],[[myArrayOfAddressBooks objectAtIndex:indexPath.row] objectForKey:@"LastName"]]; 
cell.detailTextLabel.text = 

[NSString stringWithFormat:@"%@", [[myArrayOfAddressBooks objectAtIndex:indexPath.row] objectForKey:@"Address"]]; 

return cell; 

}

在我做了选择行的索引路径我有这样的:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *cell; 
cell = [self.tableView cellForRowAtIndexPath: indexPath]; 

if ([[myArrayOfAddressBooks objectAtIndex:indexPath.row] objectForKey:@"emailSelected"] != @"YES") 
{  
cell.accessoryType = UITableViewCellAccessoryCheckmark; 
[[myArrayOfAddressBooks objectAtIndex:indexPath.row] setValue:@"YES" forKey:@"emailSelected"]; 
} 
else 
{  
    cell.accessoryType = UITableViewCellAccessoryNone; 
    [[myArrayOfAddressBooks objectAtIndex:indexPath.row] setValue:@"NO" forKey:@"emailSelected"]; 
}  

回答

6

这是由于如何UITableView “回收” UITableViewCell出于提高效率的目的,以及您在选择细胞时如何标记细胞。

您需要为在tableView:cellForRowAtIndexPath:内处理/创建的每个单元格刷新/设置accessoryType值。您如何正确在myArrayOfAddressBooks数据结构更新的状态,你只需要在tableView:cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

{ 
    static NSString *CellIdentifier = @"MyCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    NSDictionary *info = [myArrayOfAddressBooks objectAtIndex:indexPath.row]; 

    cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", [info objectForKey:@"FirstName"],[info objectForKey:@"LastName"]]; 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", [info objectForKey:@"Address"]]; 

    cell.accessoryType = ([[info objectForKey:@"emailSelected"] isEqualString:@"YES"]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; 

    return cell; 
} 

使用这些信息此外,除非有很好的理由保存状态@"Yes"@"No"串,为什么不救他们如[NSNumber numberWithBool:YES][NSNumber numberWithBool:NO]?当您想要进行比较时,这将简化您的逻辑,而不必一直使用isEqualToString:

例如

cell.accessoryType = ([[info objectForKey:@"emailSelected"] boolValue]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;