views:

203

answers:

4

I would just like to clarify that by 'design', I mean software design, not UI design.

I have an application similar to the native settings app. The problem I have with it is it doesn't follow the same clear-cut MVC style. Other apps tend to focus around displaying one kind of thing. In the case of a periodic table app for example, it's elements. The elements clearly comprise the model, and they share similar properties and behaviours, meaning they can be displayed and interacted with identically. An app like this almost designs itself!

My app, like the settings apps, consists of an arbitrary selection of rows displaying dissimilar data in dissimilar ways. One row might contain a switch, the other might modally present a very specific view when tapped. They're all very different.

How do you design something like this?

At the moment, I'm doing it all in the view controller, and the relevant rows are being tracked via an enum:

enum {
    kNameRow,
    kGenderRow,
    kJobTypeRow,
    kLevelOfExerciseRow,
    kEmailAddressRow,
    kTelephoneNumberRow
};

As I described, these cells are all very different, so displaying cells is handled like this:

// - tableView:cellForRowAtIndexPath pseudocode.

switch (indexPath.row) {
    case kNameRow: // create name cell.
    case kGenderRow: // create gender cell.
    case kJobTypeRow: // create job type cell.
    case kLevelOfExerciseRow: // create level of exercise cell.
    case kEmailAddressRow: // create email address cell.
    case kTelephoneNumberRow: // create telephone number cell.
}

And interacting with cells is handled similarly:

// - tableView:didSelectRowAtIndexPath pseudocode.

switch (indexPath.row) {
    case kNameRow: // do name-specific stuff.
    case kGenderRow: // do gender-specific stuff.
    case kJobTypeRow: // do job type-specific stuff.
    case kLevelOfExerciseRow: // do level of exercise-specific stuff.
    case kEmailAddressRow: // do email address-specific stuff.
    case kTelephoneNumberRow: // do telephone number-specific stuff.
}

This seems hugely unwieldy, and has the added of problem of not working when the table is broken down into multiple sections.

Is there a better way to do this? Are there any design patterns I would benefit from using when working with big tables of largely unrelated data?

Any tips at all are hugely appreciated.

+2  A: 

you might want to look at coreyfloyds project http://github.com/coreyfloyd/Generic-Heterogeneous-Table-Views i think this might have the functionality you need.

Gauloises
This is some decent code and an interesting take on how to manage an app like this. Unfortunately, it only really supports standard 'fields', like text input, radio selection lists, etc. My app does other much specific stuff, so I can't really use it. All I'm looking for is general tips for designing an infinitely extensible app which deals with an arbitrary number of rows displaying unrelated data in very different ways.
David Foster
+2  A: 

Here's my suggestion - handle each cell as a member of the view.

lol, it's been a while since I've used a table, so I could just be talkin' crap here but give it a try.

instead of an enum use:

NSThingyCell *nameRow;
NSThingyCell *genderRow;

@property IBOutlet NSThingyCell *nameRow;
@property IBOutlet NSThingyCell *genderRow;

- (IBAction) nameRowChanged:(id)sender;
- (IBAction) genderRowChanged:(id)sender;

and then instead of a table call with a switch, just wire each individual cell up in Interface Builder.

This has the added benefit of being row-independent, so if you have to put "ageRow" in between name and gender, nothing gets screwed up.

This will also get pretty big, so if your view has several tables, you may want to consider splitting those tables out into separate nibs/controllers and loading the views at run-time.

Stephen Furlani
You could also take a look at a similar question I had:http://stackoverflow.com/questions/3343702/cocoa-nstabview-coding-style-question/3346725#3346725
Stephen Furlani
+2  A: 

I've become fond of implementing section controllers that pull the logic out of you UITableViewController subclass (or other hosting controller) and move them into self-contained classes.

I ended up implementing a base protocol that defines what a section controller needs to do - for me, that includes the number of rows in a section and a cell for the row (don't need the whole index path since the controller deals with a single section). I've got optional method for returning a section name and row height. That's all I've implemented so far since that's all I've actually needed.

It works for me because my individual sections tend to be homogeneous, but you could easily use the idea to return heterogeneous cells within the same section or refactor the idea to have cell type controllers instead of section controllers. In the end, my UITableViewDelegate and UITableViewDataSource methods just need to figure out which section controller to call instead of embedded all the logic within the UITableViewController subclass.

I think I got the idea from this article, but I also saw a more recent article that describes the same idea.

Jablair
+2  A: 

Have you ever thought of simply having an array of objects for a class which contains a UI element and some other identifiable data?

@interface settingsOption {
    NSString *key;
    UIView *displayElement;
}

+ (settingsOption *)optionWithKey:(NSString *)key andDisplayElement:(UIView *)displayElement;

@property (nonatomic, retain) UIView *displayElement;
@property (nonatomic, retain) NSString *key;

@end

Where the class method would look like

+ (settingsOption *)optionWithKey:(NSString *)key andDisplayElement:(UIView *)displayElement;
    settingsOption *option = [[settingsOption alloc] init];
    option.key = key;
    option.displayElement = displayElement;
    return [option autorelease];
}

Your settings class would have an array of settingsOption instances.

- (void)somewhereInMySettingsClass
    mySettings = [[NSMutableArray alloc] init];
    [mySettings addObject:[settingsOption optionWithKey:@"age" andDisplayElement:[UIButton buttonWithStyle:UIButtonStyleRect]]];

    [mySettings addObject:...];
}

The table's cellForRowAtIndexPath would just do

[cell addSubview:[[mySettings objectAtIndex:indexPath.row] displayElement]];

You were talking about sections, though, which would add another layer to the data. This might simply be a matter of splitting mySettings into an array of arrays instead, where each array in the array is one section.

Not sure if I missed anything above. Feel free to point and poke.

You might simplify the settingsOption class further by adding more helper classes for various types of elements, e.g.

+ (settingsOption *)buttonWithKey:(NSString *)key;
+ (settingsOption *)switchWithKey:(NSString *)key;
+ (settingsOption *)pickerWithKey:(NSString *)key withDataSource:(id <UIPickerViewDataSource>)source withDelegate:(id <UIPickerViewDelegate>)delegate;

etc etc.

Kalle