topgun
topgun

Reputation: 2573

UITableView multiple section with multiple dictionaries

I am trying to work out on this scenario and just curious if it's possible to have multiple sections in one UITableView with with multiple NSDictionary contents? I have been trying to figure this and see if someone have had encountered this issue and how did s/he could get it resolved?

I have segregated count in numberOfRowsInSection method, but at cellForRowAtIndexPath I am having some issues with respect to how to segregate the sections with different dictionaries.

Upvotes: 0

Views: 456

Answers (2)

Vincent Bernier
Vincent Bernier

Reputation: 8664

If I understand correctly you are using 1 dictionary per section.

Dictionary are unordered, so you must keep in some way the key in a ordered fashion.
You can put your Dictionary in an array in the order you want to show them. So that's for the section part of your NSIndexPath.

aDictionary = [arrayOfDic objectAtIndex:indexPath.section];

If you want to display the content of that dictionary in ascending order or your key you can do something like that :

anArray = [[aDictionary allKeys] sortedInSomeWayWithSomeSelector];
someObjt = [anArray objectAtIndex:indexPath.row];

Then you have the row for your NSIndexPath.
This is pretty generic, you will have to adapt it to your code.
And I would prefer to cache the orderedKey, because sorting at every row would be a waste of resources, but you got the general idea.

Upvotes: 1

Rayfleck
Rayfleck

Reputation: 12106

CellForRowAtIndexPath receives the indexPath parameter as an input. The indexPath.section attribute tells you what section of the table the cell is in, and indexPath.row tells you what row within that section. If you have a separate dictionary for each section, then you do a switch on indexPath.section, and get the data from the appropriate dictionary for that row.

Upvotes: 1

Related Questions