使用UITableView indexPath访问NSMutableArrays的多维NSMutableArray
我有一个dynamic的部分和行的UITableView
,我想要使用自定义cell.accessoryView
,我已经设法使用下面的代码创build。
- (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; }
问题是这样的,它添加了一个刻度到每一个有行剔的部分…这是不是我想要的,所以我改变了上面的代码工作NSMutableArray
的NSMutableArrays
。
但是现在我得到一个错误
- (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; }
当我去显示第一个单元格的应用程序时,会出现以下错误
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
我从我的parsing器委托获取值selectedStatesMArray
像这样。 这个代码在cellForRow
代码被调用之前调用,所以它应该工作,但不是。
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];
任何帮助,将不胜感激。
你的问题是你有一个空数组,而不是尝试和解读你的代码,我build议你使用一个NSMutableIndexSet
的数组。 这会让你的生活变得更轻松。
我们从初始化方法开始。 您应该在完成加载数据之后,在调用tableview上的reloadData
之前立即调用它。
@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
将是 –
- (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; }