我有一个动态数量的部分和行的UITableView
,我想使用我设法使用以下代码创建的自定义cell.accessoryView
。
- (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
{{1} }}
但现在我收到错误
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'
。在调用selectedStatesMArray
代码之前调用此代码,因此它应该正常工作但不是。
cellForRow
答案 0 :(得分:1)
你的问题是你有一个空数组但不是尝试解密你的代码,我建议你使用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;
}