Skip to content

Instantly share code, notes, and snippets.

@lukeredpath
Created April 22, 2010 16:46
Show Gist options
  • Select an option

  • Save lukeredpath/375472 to your computer and use it in GitHub Desktop.

Select an option

Save lukeredpath/375472 to your computer and use it in GitHub Desktop.
A handy collection class for use with UITableView data sources, with filtering and indexing support baked in.
//
// LRTableViewCollection.h
// TellYouGov
//
// Created by Luke Redpath on 19/04/2010.
// Copyright 2010 LJR Software Limited. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface LRTableViewCollection : NSObject {
NSArray *objects;
NSArray *indexedObjects;
NSArray *filteredObjects;
UILocalizedIndexedCollation *collation;
SEL indexSelector;
NSPredicate *filterPredicate;
}
@property (nonatomic, copy) NSArray *objects;
@property (nonatomic, retain) UILocalizedIndexedCollation *collation;
@property (nonatomic, assign) SEL indexSelector;
@property (nonatomic, retain) NSPredicate *filterPredicate;
@property (nonatomic, readonly) NSInteger sectionCount;
- (NSInteger)numberOfRowsInSection:(NSInteger)section;
- (id)objectAtIndexPath:(NSIndexPath *)indexPath;
- (void)appendObjects:(NSArray *)moreObjects;
@end
//
// LRTableViewCollection.m
// TellYouGov
//
// Created by Luke Redpath on 19/04/2010.
// Copyright 2010 LJR Software Limited. All rights reserved.
//
#import "LRTableViewCollection.h"
#import "NSArray+Indexing.h"
// NSArray+Indexing can be found at http://gist.github.com/375409
@implementation LRTableViewCollection
@synthesize objects;
@synthesize collation;
@synthesize indexSelector;
@synthesize filterPredicate;
@dynamic sectionCount;
- (void)dealloc {
[objects release];
[indexedObjects release];
[filteredObjects release];
[collation release];
[filterPredicate release];
[super dealloc];
}
- (id)init;
{
if (self = [super init]) {
objects = [[NSArray alloc] init];
}
return self;
}
- (void)setObjects:(NSArray *)array;
{
[objects release]; objects = [array copy];
if (self.collation) {
indexedObjects = [[objects indexUsingCollation:self.collation withSelector:indexSelector] retain];
}
}
- (void)appendObjects:(NSArray *)moreObjects;
{
self.objects = [objects arrayByAddingObjectsFromArray:moreObjects];
}
- (void)setFilterPredicate:(NSPredicate *)predicate;
{
if (predicate != filterPredicate) {
[filterPredicate release]; filterPredicate = [predicate retain];
if (filterPredicate == nil) {
[filteredObjects release]; filteredObjects = nil;
} else {
filteredObjects = [[self.objects filteredArrayUsingPredicate:self.filterPredicate] retain];
}
}
}
#pragma mark -
#pragma mark Dynamic collection properties
- (NSInteger)sectionCount;
{
if (self.filterPredicate == nil && indexedObjects != nil) {
return [indexedObjects count];
}
return 1;
}
- (NSInteger)numberOfRowsInSection:(NSInteger)section;
{
if (self.filterPredicate) {
return [filteredObjects count];
} else if (indexedObjects != nil) {
return [[indexedObjects objectAtIndex:section] count];
}
return [objects count];
}
- (id)objectAtIndexPath:(NSIndexPath *)indexPath;
{
if (self.filterPredicate) {
return [filteredObjects objectAtIndex:indexPath.row];
} else if (indexedObjects != nil) {
return [[indexedObjects objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
}
return [objects objectAtIndex:indexPath.row];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment