【问题标题】:Objective c - how can I add checked/unchecked value of checkbox in arrayObjective c - 如何在数组中添加复选框的选中/未选中值
【发布时间】:2023-03-20 21:22:01
【问题描述】:

如果选中复选框,我想保存值 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;
}

【问题讨论】:

  • 你试过上面的代码了吗?结果如何?

标签: objective-c checkbox


【解决方案1】:
[cheval insertObject:(checked) ? @"FALSE":@"TRUE" atIndex:indexPath.row];

这当前将值的字符串表示形式添加到cheval 数组。您可以将其更改为:

[cheval insertObject:@(checked) atIndex:indexPath.row];

将字符串更改为NSNumber(这样您就可以调用boolValue)。

【讨论】:

    【解决方案2】:

    试试这个:

    [array2 insertObject:[NSNumber numberWithBool:[checkButton state]] atIndex:indexPath.row];
    

    checkButton 是复选框的出口。

    【讨论】:

      【解决方案3】:
      1. NSMutableArray 不像 C 数组。它更像是一个列表。所以

        BOOL 已检查 = [[checkedArr objectAtIndex:indexPath.row] boolValue]; [checkedArr removeObjectAtIndex:indexPath.row];

      不会像你想要的那样工作。

      1. 如果cheval 包含的对象少于indexPath.row[cheval insertObject:(checked) ? @"FALSE":@"TRUE" atIndex:indexPath.row]; 也会崩溃。

      我建议使用NSMutableDictionary 而不是NSMutableArray。您可以使用NSIndexPath 对象作为键。并改变一点算法:

      - (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;
      }
      

      【讨论】:

      • 我想使用自定义的复选标记,这样小图标会显示为选中,其他图像显示为未选中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      • 2019-08-10
      • 1970-01-01
      • 2011-11-16
      相关资源
      最近更新 更多