Added support for grouping objects into sections within RKTableController. closes #658

This commit is contained in:
Blake Watters
2012-04-10 11:34:22 -04:00
parent b27be1cbc5
commit b282e3a813
10 changed files with 227 additions and 60 deletions

View File

@@ -48,7 +48,6 @@
After the block is invoked, the objects will be loaded into the specified section.
*/
// TODO: Update comments...
- (void)loadTableItems:(NSArray *)tableItems withMappingBlock:(void (^)(RKTableViewCellMapping *))block; // TODO: Eliminate...
- (void)loadTableItems:(NSArray *)tableItems withMapping:(RKTableViewCellMapping *)cellMapping;
- (void)loadTableItems:(NSArray *)tableItems
inSection:(NSUInteger)sectionIndex
@@ -103,6 +102,11 @@
/// @name Managing Sections
/////////////////////////////////////////////////////////////////////////
/**
The key path on the loaded objects used to determine the section they belong to.
*/
@property(nonatomic, copy) NSString *sectionNameKeyPath;
// Coalesces a series of table view updates performed within the block into
// a single animation using beginUpdates: and endUpdates: on the table view
// TODO: Move to super-class?
@@ -113,12 +117,6 @@
// NOTE: connects cellMappings if section.cellMappings is nil...
- (void)addSection:(RKTableSection *)section;
/**
Creates an section and yields it to the block for configuration. After the block
is evaluated, the section is added to the table.
*/
- (void)addSectionUsingBlock:(void (^)(RKTableSection *section))block;
/** Inserts a new section at the specified index.
* @param section Must be a valid non nil RKTableViewSection.
* @param index Must be less than the total number of sections. */

View File

@@ -22,6 +22,7 @@
#import "RKAbstractTableController_Internals.h"
#import "RKLog.h"
#import "RKFormSection.h"
#import "NSArray+RKAdditions.h"
// Define logging component
#undef RKLogComponent
@@ -30,6 +31,7 @@
@implementation RKTableController
@synthesize form = _form;
@synthesize sectionNameKeyPath = _sectionNameKeyPath;
#pragma mark - Instantiation
@@ -51,7 +53,8 @@
- (void)dealloc {
[self removeObserver:self forKeyPath:@"sections"];
[_form release];
[_sectionNameKeyPath release];
[super dealloc];
}
@@ -93,10 +96,6 @@
}
}
- (void)addSectionUsingBlock:(void (^)(RKTableSection *section))block {
[self addSection:[RKTableSection sectionUsingBlock:block]];
}
- (void)removeSection:(RKTableSection *)section {
NSAssert(section, @"Cannot remove a nil section");
if ([self.sections containsObject:section] && self.sectionCount == 1) {
@@ -179,7 +178,7 @@
return [mutableObjects autorelease];
}
// TODO: NOTE - Everything currently needs to pass through this method to pick up header/footer rows...
// NOTE - Everything currently needs to pass through this method to pick up header/footer rows...
- (void)loadObjects:(NSArray *)objects inSection:(NSUInteger)sectionIndex {
// Clear any existing error state from the table
self.error = nil;
@@ -241,10 +240,6 @@
[self loadTableItems:tableItems inSection:0];
}
- (void)loadTableItems:(NSArray *)tableItems withMappingBlock:(void (^)(RKTableViewCellMapping*))block {
[self loadTableItems:tableItems inSection:0 withMapping:[RKTableViewCellMapping cellMappingUsingBlock:block]];
}
#pragma mark - Network Table Loading
- (void)loadTableFromResourcePath:(NSString*)resourcePath {
@@ -275,12 +270,6 @@
[self addSection:(RKTableSection *)section];
}
// TODO: How to handle animating loading a replacement form?
// if (self.loaded) {
// NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [form.sections count] - 1)];
// [self.tableView reloadSections:indexSet withRowAnimation:self.defaultRowAnimation];
// }
[self didFinishLoad];
[form didLoadInTableController:self];
}
@@ -331,15 +320,25 @@
- (void)objectLoader:(RKObjectLoader *)loader didLoadObjects:(NSArray *)objects {
// TODO: Could not get the KVO to work without a boolean property...
// TODO: Need to figure out how to group the objects into sections
// TODO: Apply any sorting...
// Load them into the first section for now
[self loadObjects:objects inSection:0];
if (self.sectionNameKeyPath) {
[self removeAllSections];
NSArray *sectionedObjects = [objects sectionsGroupedByKeyPath:self.sectionNameKeyPath];
for (NSArray *sectionOfObjects in sectionedObjects) {
NSUInteger sectionIndex = [sectionedObjects indexOfObject:sectionOfObjects];
if (sectionIndex >= [self sectionCount]) {
[self addSection:[RKTableSection section]];
}
[self loadObjects:sectionOfObjects inSection:sectionIndex];
}
} else {
[self loadObjects:objects inSection:0];
}
}
- (void)reloadRowForObject:(id)object withRowAnimation:(UITableViewRowAnimation)rowAnimation {
// TODO: Find the indexPath of the object...
NSIndexPath *indexPath = [self indexPathForObject:object];
if (indexPath) {
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:rowAnimation];