在NSMutableArray中使用BOOL对象来确定单元状态

我正在使用一个引用为’stateArray’的NSMutableArray 。 stateArray需要简单地保存我的单元格的BOOL值,以确定它们是否被选中。 这是我的代码..

我的.h中的stateArray:

 @property (nonatomic, strong) NSMutableArray *stateArray; 

然后stateArray 没有合成。 它需要在整个数组中填充NO ,这样当单元格被选中时,NO可以被YES替换。 目前,此代码为每个单元格的stateArray打印0(NSLog在我的cellForRowAtIndexPath:中的if (showCheckmark == YES)

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { [[UITableViewCell appearance] setTintColor:[UIColor colorWithHexString:@"#669900"]]; #define CHECK_NULL_STRING(str) ([str isKindOfClass:[NSNull class]] || !str)?@"":str static NSString *CellIdentifier = @"inviteCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; //Customization cell.textLabel.highlightedTextColor = [UIColor colorWithHexString:@"#669900"]; cell.selectionStyle = UITableViewCellSelectionStyleGray; cell.backgroundColor = [UIColor blackColor]; cell.textLabel.textColor = [UIColor whiteColor]; //Ignore this, it's for UISearchBar BOOL isSearching = tableView != self.tableView; NSArray *arrayToUse = (isSearching ? searchResults : contactsObjects); id p = arrayToUse[indexPath.row]; NSString *fName = (__bridge_transfer NSString *)(ABRecordCopyValue((__bridge ABRecordRef)(p), kABPersonSortByFirstName)); NSString *lName = (__bridge_transfer NSString *)(ABRecordCopyValue((__bridge ABRecordRef)(p), kABPersonSortByLastName)); cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", CHECK_NULL_STRING(fName), CHECK_NULL_STRING(lName)]; _stateArray = [NSMutableArray array]; for (int i = 0 ; i != contactsObjects.count ; i++) [_stateArray addObject:@(NO)]; BOOL showCheckmark = [[_stateArray objectAtIndex:indexPath.row] boolValue]; if (showCheckmark == YES) { cell.accessoryType = UITableViewCellAccessoryCheckmark; NSLog(@"It hit showCheckmark = YES, and stateArray is %@",_stateArray[indexPath.row]); } else { cell.accessoryType = UITableViewCellAccessoryNone; NSLog(@"It hit showCheckmark = NO, and stateArray is %@",_stateArray[indexPath.row]); } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; { id object = contactsObjects[indexPath.row]; UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if (cell.accessoryType == UITableViewCellAccessoryNone) { cell.accessoryType = UITableViewCellAccessoryCheckmark; [_stateArray replaceObjectAtIndex:indexPath.row withObject:@(YES)]; [selectedObjects addObject:object]; } else { cell.accessoryType = UITableViewCellAccessoryNone; [_stateArray replaceObjectAtIndex:indexPath.row withObject:@(NO)]; [selectedObjects removeObject:object]; } //slow-motion selection animation. [tableView deselectRowAtIndexPath:indexPath animated:YES]; } 

要跟踪选定的项目,请使用Dictionary而不是NSMutableArray ,并将indexPath.row保持为Key,并选择为curresponding Value

而不是使用BOOL's数组执行这些操作,您可以更新代码,如下所示。

 @property (nonatomic, strong) NSMutableDictionary * selectedRowCollection; - (void)viewDidLoad{ self.selectedRowCollection = [[NSMutableDictionary alloc] init]; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; { id object = contactsObjects[indexPath.row]; UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if (cell.accessoryType == UITableViewCellAccessoryNone) { cell.accessoryType = UITableViewCellAccessoryCheckmark; [self.selectedRowCollection setObject:@"1" forKey:[NSString stringWithFormat:@"%d",indexPath.row]]; } else { cell.accessoryType = UITableViewCellAccessoryNone; [self.selectedRowCollection removeObjectForKey:[NSString stringWithFormat:@"%d",indexPath.row]]; } //slow-motion selection animation. [tableView deselectRowAtIndexPath:indexPath animated:YES]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { BOOL showCheckmark = [[self.selectedRowCollection valueForKey:[NSString stringWithFormat:@"%d",indexPath.row]] boolValue]; if (showCheckmark == YES) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; } } 

注意:在重新加载新的tableview数据集时,不要忘记从字典中删除字典项。

BOOL值用NSNumber包装,这就是你得到0的原因:

 _stateArray = [NSMutableArray array]; for (int i = 0 ; i != contactsObjects.count ; i++) [_stateArray addObject:@(NO)]; BOOL showCheckmark = [[_stateArray objectAtIndex:indexPath.row] boolValue]; if (showCheckmark == YES) { cell.accessoryType = UITableViewCellAccessoryCheckmark; NSLog(@"It hit showCheckmark = YES, and stateArray is %@",[[_stateArray objectAtIndex:indexPath.row] boolValue] ? @"YES" : @"NO"); } else { cell.accessoryType = UITableViewCellAccessoryNone; NSLog(@"It hit showCheckmark = NO, and stateArray is %@",[[_stateArray objectAtIndex:indexPath.row] boolValue] ? @"YES" : @"NO"); } 

BOOL不是对象,因此您不能使用%@格式说明符。 您需要手动处理此问题

尝试这个,

更新循环以填充_stateArray

 for (int i = 0 ; i != contactsObjects.count ; i++) { [_stateArray addObject:[NSNumber numberWithBool:NO]]; }