-
-
Save blakewatters/1127828 to your computer and use it in GitHub Desktop.
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
// Configure RestKit Logging | |
RKLogConfigureByName("RestKit/UI", RKLogLevelTrace); | |
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace); | |
RKLogConfigureByName("RestKit/Network*", RKLogLevelDebug); | |
// Configure the object manager | |
RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:@"http://localhost:4567/"]; | |
[manager.mappingProvider setMapping:[RKObjectMapping mappingForClass:[Contact class] withBlock:^(RKObjectMapping* mapping) { | |
[mapping mapKeyPath:@"first_name" toAttribute:@"firstName"]; | |
[mapping mapKeyPath:@"last_name" toAttribute:@"lastName"]; | |
[mapping mapKeyPath:@"email_address" toAttribute:@"emailAddress"]; | |
}] forKeyPath:@"contacts"]; | |
__tableViewModel = [RKTableViewModel tableViewModelForTableViewController:self]; | |
[__tableViewModel mapObjectsWithClass:[Contact class] toTableViewCellClass:[UITableViewCell class] withBlock:^(RKTableViewCellMapping* cellMapping) { | |
cellMapping.cellStyle = UITableViewCellStyleSubtitle; | |
cellMapping.accessoryType = UITableViewCellAccessoryDisclosureIndicator; | |
[cellMapping mapKeyPath:@"fullName" toAttribute:@"textLabel.text"]; | |
[cellMapping mapKeyPath:@"emailAddress" toAttribute:@"detailTextLabel.text"]; | |
cellMapping.onSelectCellForObject = ^ (UITableViewCell* cell, Contact* contact) { | |
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Cell Selected!" | |
message:[NSString stringWithFormat:@"You selected '%@'", contact.fullName] | |
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; | |
[alertView show]; | |
}; | |
// TODO: configureCell:forObject:atIndexPath: | |
}]; | |
[__tableViewModel loadTableFromResourcePath:@"/contacts.json"]; | |
} |
Greg -
This afternoon I was actually working on the Loading/Error/Empty states within the model. Continuing the trend of trying to stay out of the inheritance hierarchy and let you do your own thing, I am adding delegate hooks for the events and there will be support for showing/hiding an overlay (but you can nil the views to disable). In GateGuru we also make heavy use of table and footer items and support for that will be baked in, so you can imagine hooking into the delegates for your Loading/Finished loading and just hiding/showing the loading cells as you feel appropriate.
For the built in display support, I am anticipating drawing an overlay that matches the frame of the table and then drawing the views into it.
We are definitely also going to add first-class support for using XIBs for the table cells as well -- I'll get that in place once I finish the foundational work next week. What I would really like to wind up with a month or so down the road is the ability to generate forms for individual objects in addition to the collection work already in progress. I'd like to be able to do the full Master > Detail application style with object mapping driven table views.
This is total pseudo-code (the code above already works btw), but you could imagine something like...
[RKTableViewFormModel modelForObject:user withBlock:^(RKTableViewFormModel* model) {
[model addTextFieldForAttribute:@"login" withLabel:@"Login"];
[model addTextFieldForAttribute:@"password" withLabel:@"Password" secure:YES];
[model addSubmitButtonWithLabel:@"Login" onSubmitBlock:^(User* user) {
[user login];
}];
}];
I love it. We can still use custom cell classes, it pulls the data attributes in automatically, we get the didSelect stuff automatically right there in a block.
One thing I've become fond of, but it might not make sense here, is a "Loading/Error ..." cell. Whenever there's rest kit network activity, I push all the cells in the table down a row, to make room for an indicator cell that alerts the user that it's still loading data, or if there was a network connectivity issue. I've never thought that the built-in network activity indicator was all that useful or explanatory.
Perhaps in the context of this new model, it might be better for me to create a separate view above or below the table view that handles this, rather than doing it within the table itself. However, given that just about everyone that uses this model will have similar needs, I figured I'd see what you think about it.