我认为您的问题可能与单元重用有关。在你的控制器中创建一个NSMutableDictionary(应该是实例变量),然后像这样创建它:
dictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithBool:NO], [NSIndexPath indexPathForRow:0 inSection:0],
[NSNumber numberWithBool:NO], [NSIndexPath indexPathForRow:1 inSection:0], nil];
对所有行执行此操作,或者如果需要,计算行及其索引,但将其所有对象设置为 [NSNumber numberWithBool:NO。
现在,tableView:didSelectRowAtIndexPath:
if ([(NSNumber *)[dictionary objectForKey:indexPath] isEqualToNumber:[NSNumber numberWithBool:NO]) {
[dictionary setObject:[NSNumber numberWithBool:YES] forKey:indexPath];
// Do any visual updating necessary
} else {
[dictionary setObject:[NSNumber numberWithBool:NO] forKey:indexPath];
// Do any visual updating necessary
}
现在我们有了一种存储单元格是否被选中的方法,更改您的单元格标识符以适用于您的单元格:
NSString *identifier = [[dictionary objectForKey:indexPath] stringValue];
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease];
}
// Do any visual updating necessary
return cell;
现在,更新您的自定义单元格类以接受 @"0" 和 @"1" 作为标识符,并根据此输入自动更新它们的选定状态。