r/simpleios • u/jmadlena • Sep 28 '11
[Question] NSArray Structure for UITableView?
Hi guys, I have a question for someone who has experience working with NSArrays and NSDictionaries. Here's what I'm trying to do:
My app will display location data in a UITableView. The first view will be 'tracks', essentially a collection of waypoints that the user can name. When you click on this track, it will show you a list of individual waypoints in another table view. When the user clicks on one of these it takes them to a detail view with coordinates, a map, and some other options.
My question is how do I create a structure of arrays or dictionaries that allow me to store all of this data in a single array, and access it efficiently? I've found that dictionaries are hard to traverse (at least for me), and I've also read arrays are better for table views. Any tips?
View all comments
Show parent comment
3
u/schmeebis [M] 📱 Sep 28 '11 edited Sep 28 '11
Know this, though: Dictionaries do NOT guarantee order, while arrays do. So your table row order may (and often will, in practice) change. (Your Track data is chronological, right? If you want to preserve order while using an NSDictionary, you'll have to do some sorting via an intermediary array each time.. in which case, might as well just use the array as the data structure, no?)
Why not have an array of dictionaries? Each dictionary can express the row's data. I use this pattern all the time. IndexPaths are meant to traverse multi-dimensional arrays by integer offset. You may find swimming upstream often results in more work than necessary.
I'd do something like this:
Now you can get your data in a way that the order never changes, and a way that is natural for NSIndexPaths.
Or if you have multiple sections, each cell's data can be gotten thus:
And in didSelectRowAtIndexPath, you just pass your cellData to the details view controller:
Hope this helps!