Home > iPhone SDK > Nested Arrays
Nested Arrays
Posted on Monday, August 31, 2009 by android apps market for tablests
I'm staying the heck away from talking about Flash for a while. ?
In my quest for a good solution for handling table-based detail editing panes, I've been experimenting with using nested arrays to drive the table structure. A nested array is nothing more than an array of arrays (and I'm talking about NSArray instances here). The main or outer array contains one instance of NSArray for each section, and each subarray contains one object for each row in the section it represents. It takes multiple nested arrays to hold the structure of a table, with one nested array holding the labels, another holding the keys, and another holding the class of the controller class that can be used to edit that item. They're paired nested arrays, I guess.
This solution isn't quite as turnkey as the property-list driven solution I was working on earlier, but it's conceptually a lot easier to explain, and it doesn't squirrel away all the code I need to demonstrate into complex, generic classes. It's a hell of a lot better than having large nested switch statements in your controller class.
The entire process I'm developing will be shown in More iPhone 3 Development, but here's a category to make it easier to pull information out of a nested array in case you want to experiment on your own. This category adds a method to NSArray that lets you retrieve the right object from the nested array for a given NSIndexPath. Note that this category, like the table views it was created to support, supports only simple index paths that store a row and a section.
NSArray-NestedArray.h
NSArray-NestedArray.m
In my quest for a good solution for handling table-based detail editing panes, I've been experimenting with using nested arrays to drive the table structure. A nested array is nothing more than an array of arrays (and I'm talking about NSArray instances here). The main or outer array contains one instance of NSArray for each section, and each subarray contains one object for each row in the section it represents. It takes multiple nested arrays to hold the structure of a table, with one nested array holding the labels, another holding the keys, and another holding the class of the controller class that can be used to edit that item. They're paired nested arrays, I guess.
This solution isn't quite as turnkey as the property-list driven solution I was working on earlier, but it's conceptually a lot easier to explain, and it doesn't squirrel away all the code I need to demonstrate into complex, generic classes. It's a hell of a lot better than having large nested switch statements in your controller class.
The entire process I'm developing will be shown in More iPhone 3 Development, but here's a category to make it easier to pull information out of a nested array in case you want to experiment on your own. This category adds a method to NSArray that lets you retrieve the right object from the nested array for a given NSIndexPath. Note that this category, like the table views it was created to support, supports only simple index paths that store a row and a section.
NSArray-NestedArray.h
//
// NSArray-NestedArrays.h
#import <Foundation/Foundation.h>
@interface NSArray(NestedArrays)
/*!
This method will return an object contained with an array
contained within this array. It is intended to allow
single-step retrieval of objects in the nested array
using an index path
*/
- (id)nestedObjectAtIndexPath:(NSIndexPath *)indexPath;
@end
NSArray-NestedArray.m
//
// NSArray-NestedArrays.m
#import "NSArray-NestedArrays.h"
@implementation NSArray(NestedArrays)
- (id)nestedObjectAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
NSUInteger section = [indexPath section];
NSArray *subArray = [self objectAtIndex:section];
if (![subArray isKindOfClass:[NSArray class]])
return nil;
if (row >= [subArray count])
return nil;
return [subArray objectAtIndex:row];
}
@end
Category Article Categories, iPhone SDK
Powered by Blogger.
Blog Archive
-
▼
2009
(496)
-
▼
August
(65)
- Local Events and Activities Gadget
- Click-to-Flash
- Flash Post Mortem
- Nested Arrays
- Twitter Quotes Gadget
- Dog and Puppy Photos Gadget
- Just a Few More Vacation Thoughts
- GoComics Social Gadget
- You Guys Rock
- Flash is Dead! Long Live Flash!
- Jail Quotes Gadget
- Google Search results optimized for feature phones...
- Google Search results optimized for feature phones...
- Daily Horoscopes Social Gadget
- The bright side of sitting in traffic: Crowdsourci...
- The bright side of sitting in traffic: Crowdsourci...
- Chess Social Gadget
- Tile Game Social Gadget
- YouTube Mobile App Expands to Five New Languages
- YouTube Mobile App Expands to Five New Languages
- To Autorotate or Not to Autorotate
- Google Apps Connector for BlackBerry Enterprise Se...
- Google Apps Connector for BlackBerry Enterprise Se...
- iPhone Sketch Book Mini-Review
- NY Times Crossword Social Gadget
- Another Pimp My Code
- Photos Social Gadget
- "Knowledge speaks but wisdom listens."
- "Knowledge speaks but wisdom listens."
- New Image Search Results for Feature Phones
- New Image Search Results for Feature Phones
- Property-List Driven Detail Editing Pane
- SCRABBLE Social Gadget
- Stamps on Colnect Gadget
- Sentiment by Newssift Gadget
- Ultimate Waterfall Gadget
- Fish Aquarium Deluxe 3D Gadget
- Weather 3D Gadget
- Interesting...
- Contact EMailer Gadget
- Animated Nature Gadget
- Nice iPhone Application Idea.
- iPhone Sketch Book
- Blocks on iPhone
- On iPhone Competitors
- Workshop Done
- The Iterative Web App: Outbox for Emails in Limbo
- The Iterative Web App: Outbox for Emails in Limbo
- Boot Camp Imminent
- OpenGL ES Update
- My Last Word on Dot Notation
- Fish Tales Gadget
- Daily Picture Sudoku Gadget
- How to Use Dot Notation and Properties
- Dot Notation Redux: Google's Style Guide
- The Dot Notation Controversy
- Translations and Xcode
- Artistic Greeting Cards Gadget
- Tycoon Games Gadget
- Multi-Row Delete in 3.0
- NinjaWords
- A Mac App Store
- Me, the Hypocrite Apparently
- iSimulate
- This Concerns Me Greatly
-
▼
August
(65)