Skip to content

Instantly share code, notes, and snippets.

@omnivector
Created April 27, 2011 17:35
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save omnivector/944748 to your computer and use it in GitHub Desktop.
Save omnivector/944748 to your computer and use it in GitHub Desktop.
Block based data source / delegate
//
// CCBlockTableViewDataSource.h
//
// Created by Tristan O'Tierney on 1/24/11.
//
#import <UIKit/UIKit.h>
@interface CCBlockTableViewDataSource : NSObject <UITableViewDataSource, UITableViewDelegate> {
NSMutableArray *sections;
NSMutableArray *lastSection;
}
- (void)beginSection;
- (UITableViewCell *)addCell:(UITableViewCell *)cell didSelectBlock:(void (^)(NSIndexPath *indexPath))block;
- (void)endSection;
- (void)removeAllSections;
// DataSource/Delegate Methods
- (NSInteger)numberOfSections;
- (NSInteger)numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
@end
//
// CCBlockTableViewDataSource.m
//
// Created by Tristan O'Tierney on 1/24/11.
//
#import "CCBlockTableViewDataSource.h"
@implementation CCBlockTableViewDataSource
#pragma mark Initialization
- (id)init;
{
if (!(self = [super init])) {
return nil;
}
sections = [[NSMutableArray alloc] init];
return self;
}
- (void)dealloc;
{
[sections release];
sections = nil;
[super dealloc];
}
#pragma mark Public Methods
- (void)beginSection;
{
lastSection = [NSMutableArray array];
[sections addObject:lastSection];
}
- (UITableViewCell *)addCell:(UITableViewCell *)cell didSelectBlock:(void (^)(NSIndexPath *indexPath))block;
{
if (!lastSection) {
return nil;
}
NSMutableDictionary *cellInfo = [NSMutableDictionary dictionary];
if (block) {
[cellInfo setObject:[[block copy] autorelease] forKey:@"block"];
}
[cellInfo setObject:cell forKey:@"cell"];
[lastSection addObject:cellInfo];
return cell;
}
- (void)endSection;
{
lastSection = nil;
}
- (void)removeAllSections;
{
[sections removeAllObjects];
lastSection = nil;
}
#pragma mark Public DataSource/Delegate Methods
- (NSInteger)numberOfSections;
{
return sections.count;
}
- (NSInteger)numberOfRowsInSection:(NSInteger)section;
{
if (section >= sections.count) {
return 0;
}
return [[sections objectAtIndex:section] count];
}
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
return [[[sections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"cell"];
}
- (void)didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
void (^block)(NSIndexPath *) = [[[sections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"block"];
if (block) {
block(indexPath);
}
}
#pragma mark UITableViewDataSource/Delegate Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return [self numberOfRowsInSection:section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
return [self cellForRowAtIndexPath:indexPath];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
[self didSelectRowAtIndexPath:indexPath];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment