couchbase/couchbase-lite-ios — pull request #228 by p2 (2013-12-28 20:49:32+00:00)
Title: Enable `CBLUITableSource` to create sections via delegate method

Change `CBLUITableSource` to enable table view sections.

One major internal change to how instances handle rows, external API still works the same but gains a `sections` property and two new delegate methods. If the delegate does **not** implement the delegate method no sections are being created. The delegate has to implement `couchTableSource:sectionizeRows:` and has to create and return sections itself.

Here's an example to create sections by `name.code`, up to the first period in _"code"_:

- (NSMutableArray *)couchTableSource:(CBLUITableSource *)source
sectionizeRows:(NSArray *)rows
{
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"document.properties.name.code" ascending:YES];
NSArray *sortedRows = [rows sortedArrayUsingDescriptors:@[sort]];

// sectionize into arrays
NSMutableArray *array = [NSMutableArray array];
NSString *previousKey = nil;
NSMutableArray *currentArray = nil;
for (CBLQueryRow *row in sortedRows) {
NSString *currentKey = row.document.properties[@"name"][@"code"];
NSUInteger location = [currentKey rangeOfString:@"."].location;
currentKey = (NSNotFound != location) ? [currentKey substringToIndex:location] : currentKey;
if (![previousKey isEqualToString:currentKey]) {
previousKey = currentKey;
currentArray = [NSMutableArray arrayWithObject:row];
[array addObject:currentArray];
}
else {
[currentArray addObject:row];
}
}

return array;
}