2
votes

I have my UITableView data organized such that the sections of the table are the array elements of a NSMutableArray. Each element itself is a NSMutablearray of NSMutableDictionary, representing the row of each section and the data for each row.

When I create this, I load all the data and create each section's NSMutableArray, then add them one at a time to the "outer" NSMutableArray. I reference the rows by:

[[[objData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"name"]

and have had no problems doing this. I think the advantage here is I can have a variable number of sections, which I need, and a variable number of rows in each section, also a requirement.

I do, however, get an error when I try to add a row to a section.

[[objData objectAtIndex:0] addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys: objectID,@"id",nameVar,@"name",nil]];

doesn't work. Is this the problem, or is there a problem with the way I've layed out the array? I'm thinking that the inner NSMutableArray is only alloted so much memory, but I would have thought the array is just an array of references and wouldn't have that issue. I could be wrong - it isn't the first time.

Thanks in advance for wrapping your head around this.

1
What error do you get? I'm thinking that the outer array is actually an NSArray, not an NSMutableArray, so you can't add an object to it. But no, there's no memory issue; you're right about the array of references. - matt
Hi matt, I just get "Program received signal:"SIGABRT". The console msg:*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object' You may be onto something in regards to the NSArray, but I'm looking at everything, and it all seems mutable. - Nelson Ko
Yep, I was right - you've got an immutable NSArray and you're trying to add an objec to it. You can't do that. - Remember, you can call your object anything you like, but objects have their own innate type - what matters is not how you type or describe it but what it really is, internally. - matt
Hi Matt, as you can see below, you are right. It happened from me extracting it from NSUSerDefaults. - Nelson Ko

1 Answers

1
votes

How are you intializing the array and the values inside it? I think you have an array that is not mutable.

Also an NSMutableArray has a variable size, calling initWithSize is completely unnecessary. The array will resize if it needs to.