在 NSMutableArray 中使用 BOOL 对象来确定单元格状态 [英] Using BOOL objects in NSMutableArray to determine cell statuses

查看:38
本文介绍了在 NSMutableArray 中使用 BOOL 对象来确定单元格状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using an NSMutableArray referenced as 'stateArray'. stateArray needs to simply hold the BOOL value for my cells to determine whether or not they are selected. here's my code..

我的 .h 中的 stateArray:

stateArray in my .h:

@property (nonatomic, strong) NSMutableArray *stateArray;

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

Then stateArray is not synthesized. It needs to be filled with NO throughout the array, so that when the cell becomes selected, NO can be replaced by YES. Currently this code is printing 0's for stateArray for every cell(the NSLog is in the if (showCheckmark == YES) from my cellForRowAtIndexPath:).

- (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,选择作为当前的Value.

To keep track on selected items, use a Dictionary instead of NSMutableArray and keep the indexPath.row as Key and selection as curresponding Value.

此外,您可以更新如下代码,而不是使用 BOOL's 数组进行这些操作.

Also instead of doing these operations with array of BOOL's , you can update the code as below.

@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 数据集时从字典中删除字典项.

Note: Don't forget remove the dictionary items from dictionary while reloading a new tableview dataset.

这篇关于在 NSMutableArray 中使用 BOOL 对象来确定单元格状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆