Reputation: 781
I'm using data from Locu that looks like this:
{
"menus": [
{
"menu_name": "Dinner",
"sections": [
{
"section_name": "Starters",
"subsections": [
{
"subsection_name": "",
"contents": [
{
"price": "6.00",
"type": "ITEM",
"name": "Soup of the Day"
},
etc...
I have no problem implementing a UITableView with this structure when there are multiple sections (they map directly to UITableView sections) and each section has one subsection that contains the items.
What I'm having a hard time with is dealing with sort of the opposite--when there is only one section, but multiple subsections.
Examples!
menu_name: Dinner
section0: Starters
subsection0: ""
(items)
section1: Salads
subsection0: ""
(items)
section2: Entrees
subsection0: ""
(items)
vs.
menu_name: Beer & Wine
section0: Beers
subsection0: On Tap
(items)
subsection1: Bottled
(items)
In these examples, the Dinner menu would have an easy mapping to UITableView sections, and numberOfSectionsInTableView: is an easy count of all the sections. But once you throw the Beer & Wine menu into the mix, assuming that all menus are in one long view, how do you count the sections for numberOfSectionsInTableView:? And how do you count the rows for numberOfRowsInSection:? Also, I don't think UITableView can even handle subsections, so what do you do there? Just show a section with no rows followed by another section?
Upvotes: 0
Views: 2534
Reputation: 6790
If you just make the sections and subsections the same, then you can get your number of things by creating a dictionary.
In the dictionary make the sections (at this point including the subsections), the keys.
Then make the items in the sections an array of strings.
For the number of sections, get an array of the dictionary's keys and then use it's count.
For the number of rows in the section, get the section name by using objectAtIndex(section)
Then using that name, create and array of AnyObject, by using objectForKey on the dictionary, where objectForKey is the sectionName that was just created.
You can use the same technique in the cell to get it's data.
Upvotes: 0
Reputation: 862
If you're looking for a simple solution, you can set the indentationLevel
(and possibly indentationWidth
) properties for your UITableViewCell depending on where they are within the hierarchy of your data. If you need more than this, you will have to customize the appearance of your UITableViewCells further as valheru mentioned.
Upvotes: 1
Reputation: 2562
You are correct. Subsections are not really supported. You'd have to get a bit creative to get it to work the way you want it to.
Essentially you'd have to 'emulate subsections'. They would just be rows like the rest of the rows but possibly make their cell look slightly different, shaded darker, than the other cells or that kind of thing.
It ends up being more maintenance however because you have to keep track of these subsections as just regular rows along wight he other rows while trying to make them look more like subsections.
Upvotes: 1