1

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];

1 Answer 1

1

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.

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]];
   }
}

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:@"[email protected]"]];
    }
    //....
}

And your cellForRowAtIndexPath: will be

- (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:@"[email protected]"]];
    }
    else {
       cell.accessoryView=nil;
    }

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

    return cell;
}
1
  • Okay sorry for the late reply I have been reading up all about NSMutableIndexSets wrote a small test program and it worked then applied what you showed me to my app and it worked perfectly!!! not only that but I learnt something new today! So thats cool! thanks for your help.
    – HurkNburkS
    Commented Aug 1, 2014 at 1:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.