使用 UITableView indexPath 访问 NSMutableArrays 的多维 NSMutableArray [英] Access multi dimension NSMutableArray of NSMutableArrays using UITableView indexPath

查看:45
本文介绍了使用 UITableView indexPath 访问 NSMutableArrays 的多维 NSMutableArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有动态数量的部分和行的 UITableView,我想使用我设法使用以下代码创建的自定义 cell.accessoryView.

I have a UITableView with a dynamic amount of sections and rows, I am wanting to use a custom cell.accessoryView which I have managed to create using the following code.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [stackRotationController pop];
    cell = (TreeCell *)[cuttingTableView cellForRowAtIndexPath:indexPath];

    UIImageView *checkmark = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"findbitting@2x.png"]];
    NSNumber *newState;

    if ([[selectedStatesMArray objectAtIndex:indexPath.row] boolValue] == NO) {
        newState = [NSNumber numberWithBool:YES];
        cell.accessoryView = checkmark;
    } else {
        newState = [NSNumber numberWithBool:NO];
        cell.accessoryView = UITableViewCellAccessoryNone;
    }
    [selectedStatesMArray replaceObjectAtIndex:indexPath.row withObject:newState];

    //....
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *currentNumber = [uniqueKeys objectAtIndex:indexPath.section];
    NSMutableArray *currentArray = [sortedDictionaryArraysForTableView objectForKey:currentNumber];
    cellDictionary = [currentArray objectAtIndex:indexPath.row];

    static NSString *CellIdentifier = @"Cell";

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[TreeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    // Configure the cell.
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UIImageView *checkmark = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"findbitting@2x.png"]];

    if ([[selectedStatesMArray objectAtIndex:indexPath.row] boolValue] == NO) {
        cell.accessoryView = UITableViewCellAccessoryNone;
    } else {
        cell.accessoryView = checkmark;
    }


    cell.itemsDictionary = cellDictionary;
    cellDictionary = nil;
    [cell drawCell];

    return cell;
}

问题在于它为每个有行勾号的部分添加了一个勾号……这不是我想要的,所以我更改了上面的代码以处理NSMutableArrayNSMutableArrays.

The issue being with this is that it added a tick to every section that have a row tick... which is not what I wanted so I changed the code above to work off an NSMutableArray of NSMutableArrays.

但是现在我得到一个错误

However now I get an error

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [stackRotationController pop];
    cell = (TreeCell *)[cuttingTableView cellForRowAtIndexPath:indexPath];

    UIImageView *checkmark = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"findbitting@2x.png"]];
    NSNumber *newState;



    NSMutableArray *tempInternalMArray = [selectedStatesMArray objectAtIndex:indexPath.section];

    if ([[tempInternalMArray objectAtIndex:indexPath.row] boolValue] == NO) {
        newState = [NSNumber numberWithBool:YES];
        cell.accessoryView = checkmark;
    } else {
        newState = [NSNumber numberWithBool:NO];
        cell.accessoryView = UITableViewCellAccessoryNone;
    }
    [tempInternalMArray replaceObjectAtIndex:indexPath.row withObject:newState];
    [selectedStatesMArray replaceObjectAtIndex:indexPath.section withObject:tempInternalMArray];

    //....
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *currentNumber = [uniqueKeys objectAtIndex:indexPath.section];
    NSMutableArray *currentArray = [sortedDictionaryArraysForTableView objectForKey:currentNumber];
    cellDictionary = [currentArray objectAtIndex:indexPath.row];

    static NSString *CellIdentifier = @"Cell";

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[TreeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    // Configure the cell.
    // cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UIImageView *checkmark = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"findbitting@2x.png"]];

    NSMutableArray *tempInternalMArray = [[selectedStatesMArray objectAtIndex:indexPath.section] mutableCopy];

    if ([[tempInternalMArray objectAtIndex:indexPath.row] boolValue] == NO) { // error happens here
        cell.accessoryView = UITableViewCellAccessoryNone;
    } else {
        cell.accessoryView = checkmark;
    }


    cell.itemsDictionary = cellDictionary;
    cellDictionary = nil;
    [cell drawCell];

    return cell;
}

当我去显示第一个单元格时,应用程序失败并出现以下错误

When I go to display the very first cell the app falls over with the following error

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'

我像这样从解析器委托中将值放入 selectedStatesMArray 中.此代码在调用 cellForRow 代码之前调用,因此它应该可以工作,但不能.

I get the values into selectedStatesMArray from my parser delegate like so. This code is called before the cellForRow code is called so it should be working but is not.

NSNumber *newState;
newState = [NSNumber numberWithBool:NO];

NSMutableArray *tempSelectedStatesMArray = [[NSMutableArray alloc] init];
for (int i = 0; i < [sortedDictionaryArraysForTableView count]; i++) {
    NSMutableArray *tempSectionSelectedStatsMArray = [[NSMutableArray alloc] init];
    for (int j = 0; j < [[sortedDictionaryArraysForTableView objectForKey:[NSString stringWithFormat:@"%d",i]] count]; j++) {
        [tempSectionSelectedStatsMArray addObject:newState];
    }
    [tempSelectedStatesMArray addObject:tempSectionSelectedStatsMArray];

}
selectedStatesMArray = [tempSelectedStatesMArray mutableCopy];

推荐答案

你的问题是你有一个空数组,但我建议你使用 NSMutableIndexSets.它会让你的生活更轻松.

Your problem is that you have an empty array but rather than try and unscramble your code, I would suggest you use an array of NSMutableIndexSets. It will make life much easier for you.

让我们从初始化方法开始.您应该在加载完数据后立即调用它,但在对 tableview 上的 reloadData 进行任何调用之前.

Let's start with the initialisation method. You should call this as soon as you have finished loading data but before any call to reloadData on the tableview.

@property (nonatomic,strong) NSMutableArray *selectStates;

-(void) initialiseSelectStates {
   self.selectedStates=[NSMutableArray new];
   for (int i=0;i<uniqueKeys.count;i++) {
       [self.selectedStates addObject:[NSMutableIndexSet new]];
   }
}

现在,您的 didSelectRowAtIndexPath 将是 -

Now, your didSelectRowAtIndexPath will be -

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [stackRotationController pop];
    cell = (TreeCell *)[cuttingTableView cellForRowAtIndexPath:indexPath];

    NSMutableIndexSet *sectionSelects=[self.selectedStats objectAtIndex:indexPath.section];

    if ([sectionSelects containsIndex:indexPath.row]) {
       [selectionSelects removeIndex:indexPath.row];
       cell.accessoryView=nil;
    }
    else {
       [selectionSelects addIndex:indexPath.row];
       cell.accessoryView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"findbitting@2x.png"]];
    }
    //....
}

而您的 cellForRowAtIndexPath: 将是

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *currentNumber = [uniqueKeys objectAtIndex:indexPath.section];
    NSMutableArray *currentArray = [sortedDictionaryArraysForTableView objectForKey:currentNumber];
    NSMutableIndexSet *sectionSelects=[self.selectedStats objectAtIndex:indexPath.section];

    static NSString *CellIdentifier = @"Cell";

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[TreeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    // Configure the cell.
    // cell.selectionStyle = UITableViewCellSelectionStyleNone;

    if ([sectionSelects containsIndex:indexPath.row]) { 
       cell.accessoryView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"findbitting@2x.png"]];
    }
    else {
       cell.accessoryView=nil;
    }

    cell.itemsDictionary = (NSDictionary *)[currentArray objectAtIndex:indexPath.row];
    [cell drawCell];

    return cell;
}

这篇关于使用 UITableView indexPath 访问 NSMutableArrays 的多维 NSMutableArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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