Objective c 目标c-如何在数组中添加复选框的选中/未选中值

Objective c 目标c-如何在数组中添加复选框的选中/未选中值,objective-c,checkbox,Objective C,Checkbox,如果复选框被选中,我想保存值TRUE;如果在数组中未选中,我想保存值FALSE,我该怎么做呢。我已经在tableview中实现了复选框。代码是 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { BOOL checked = [[checkedArr objectAtIndex:indexPath.row] boolValue]; [checke

如果复选框被选中,我想保存值TRUE;如果在数组中未选中,我想保存值FALSE,我该怎么做呢。我已经在tableview中实现了复选框。代码是

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BOOL checked = [[checkedArr objectAtIndex:indexPath.row] boolValue];
    [checkedArr removeObjectAtIndex:indexPath.row];
    [cheval insertObject:(checked) ? @"FALSE":@"TRUE" atIndex:indexPath.row];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UIButton *button = (UIButton *)cell.accessoryView;
    UIImage *newImage = (checked) ? [UIImage imageNamed:@"tick.png"] : [UIImage imageNamed:@"white_bg.png"];
    [button setBackgroundImage:newImage forState:UIControlStateNormal];
    cell.accessoryView=button;
}
这将当前向
cheval
数组添加值的字符串表示形式。您可以将此更改为:

[cheval insertObject:@(checked) atIndex:indexPath.row];
要将字符串更改为
NSNumber
(以便您可以在其上调用
boolValue

请尝试以下操作:

[array2 insertObject:[NSNumber numberWithBool:[checkButton state]] atIndex:indexPath.row];
checkButton是复选框的出口

  • NSMutableArray与C数组不同。它更像是一个列表。所以

    BOOL checked=[[checkedArr objectAtIndex:indexath.row]boolValue]; [checkedArr removeObjectAtIndex:indexPath.row]

  • 不会像你想的那样工作

  • 另外
    [cheval insertObject:(选中)@“FALSE”:@“TRUE”atIndex:indexath.row]cheval
    包含的对象少于
    indexPath.row
    ,则code>将崩溃
  • 我建议使用
    NSMutableDictionary
    而不是
    NSMutableArray
    。您可以使用
    nsindepath
    对象作为键。并更改一点算法:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSIndexPath *indexPathKey = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]; // we use this code because in iOS 7 indexPath has type UIMutableIndexPath
        BOOL checked = ![checkedDict[indexPathKey] boolValue];
        checkedDict[indexPathKey] = @(checked);
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        UIButton *button = (UIButton *)cell.accessoryView;
        UIImage *newImage = (checked) ? [UIImage imageNamed:@"tick.png"] : [UIImage imageNamed:@"white_bg.png"];
        [button setBackgroundImage:newImage forState:UIControlStateNormal];
        cell.accessoryView=button;
    }
    
    还有,为什么不使用默认复选标记呢。如果使用它,则应按以下方式更改代码:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSIndexPath *indexPathKey = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]; // we use this code because in iOS 7 indexPath has type UIMutableIndexPath
        BOOL checked = ![checkedDict[indexPathKey] boolValue];
        checkedDict[indexPathKey] = @(checked);
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.accessoryType = checked ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    }
    

    你试过上面的代码吗?结果是什么?我想使用一个自定义复选标记,这样小图标将显示为选中,其他图像将显示为未选中。
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSIndexPath *indexPathKey = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]; // we use this code because in iOS 7 indexPath has type UIMutableIndexPath
        BOOL checked = ![checkedDict[indexPathKey] boolValue];
        checkedDict[indexPathKey] = @(checked);
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.accessoryType = checked ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    }