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:@"[email protected]"]];
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:@"[email protected]"]];
if ([[selectedStatesMArray objectAtIndex:indexPath.row] boolValue] == NO) {
cell.accessoryView = UITableViewCellAccessoryNone;
} else {
cell.accessoryView = checkmark;
}
cell.itemsDictionary = cellDictionary;
cellDictionary = nil;
[cell drawCell];
return cell;
}
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:@"[email protected]"]];
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:@"[email protected]"]];
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'
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];