-1

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

3 回答 3

0
  1. NSMutableArray is not like C array. It's more like a list. So

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

will not work like you want.

  1. Also [cheval insertObject:(checked) ? @"FALSE":@"TRUE" atIndex:indexPath.row]; will crash if cheval contains less objects than indexPath.row.

I would suggest using NSMutableDictionary instead of NSMutableArray. You may use an NSIndexPath object as a key. And change a little bit algorithm:

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

Also why don't you use default checkmark. If you use it you should change your code in the following way:

- (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;
}
于 2013-10-18T09:17:16.920 回答
0

尝试这个:

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

checkButton 是复选框的出口。

于 2013-10-18T07:43:58.180 回答
0
[cheval insertObject:(checked) ? @"FALSE":@"TRUE" atIndex:indexPath.row];

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

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

将字符串更改为NSNumber(以便您可以调用boolValue它)。

于 2013-10-18T06:59:35.210 回答