Greetings StackOverflow.
I have a scenario, in which I have a UITableView with a dynamic amount of sections.
This means that the table may have 1 section, 5 sections, 100 sections, whatever.
The problem is, each section is handled and displayed in a different manner.
Section 0, for example, contains say a list of purple cells, while section 1 contains yellow cells.
Thus, I need to be able to calculate the index that each section type will end up as.
By this, I mean, assuming the priority of the sections is as follows:
Magenta, Yellow, Green, White
Then when all sections are filled, they will be in that order, M=0, Y=1, G=2, W=3.
However, when one section is missing, say Yellow, the result is as follows:
M=0, Y=-1, G=1, W=3
Thereby removing yellow from the list, and bumping down the indices for the section.
I have already built something that accomplishes this for small section sizes by using tertiary statements, but as it defines each section type manually it quickly blows out of proportion:
// Here be some dragons.
// Each macro identifies a section
#define SectionMagenta (([self shouldDisplaySection:0])?0:-1) // 1 tertiary statements
#define SectionYellow (([self shouldDisplaySection:1])?((SectionMagenta==-1)?0:1):-1) // 3 tertiary statements
#define SectionGreen (([self shouldDisplaySection:2])?((SectionMagenta==-1)?((SectionYellow==-1)?0:1):((SectionYellow==-1)?1:2)):-1) // 11 tertiary statements
#define SectionWhite (([self shouldDisplaySection:3])?((SectionMagenta==-1)?((SectionYellow==-1)?((SectionGreen==-1)?0:1):((SectionGreen==-1)?1:2)):((SectionYellow==-1)?((SectionGreen==-1)?1:2):((SectionGreen==-1)?2:3))):-1) // 63 tertiary statements
There is a pattern here. A very complex one, yes, but a pattern none the less.
My question to you all, is if you can simply this pattern into a small, probably recursive function, that I pass in the priority of the section, and it calculates the destination index.
OR
if you can create a much simpler method of achieving what I need.
For the latter, simply setting the amount of rows displayed to 0 is not sufficient, as the padding for the section is still existent.
I’ve provided a helper function–as can be seen in the copypasta code above-to check if a section should be displayed: [self shouldDisplaySection:n]
That may very well provide all you need.
Sounds like you actually want to use an Ordered Dictionary and just push the values you’re wanting onto it, then read them off in order with the numeric indexes as your section order.
A fairly good example of an ordered dictionary implemented in Obj-C is at http://cocoawithlove.com/2008/12/ordereddictionary-subclassing-cocoa.html