Skip to content

Instantly share code, notes, and snippets.

@advantis
Last active December 20, 2015 10:59
Show Gist options
  • Save advantis/6120032 to your computer and use it in GitHub Desktop.
Save advantis/6120032 to your computer and use it in GitHub Desktop.
Generic UITableView/UICollectionView data source for objects in NSArray
//
// Copyright © 2013 Yuri Kotov
//
#import <Foundation/Foundation.h>
#import "ADVIndexedSubscripting.h"
typedef void(^ADVCellConfigurationBlock)(id cell, id object);
@interface ADVArrayDataSource : NSObject <ADVIndexedSubscripting, UITableViewDataSource, UICollectionViewDataSource>
@property (readonly, nonatomic) NSArray *array;
- (instancetype) initWithArray:(NSArray *)array
reusableCellIdentifier:(NSString *)identifier
cellConfigurationBlock:(ADVCellConfigurationBlock)block;
@end
//
// Copyright © 2013 Yuri Kotov
//
#import "ADVArrayDataSource.h"
@interface ADVArrayDataSource ()
@property (readonly, nonatomic) NSString *cellIdentifier;
@property (readonly, nonatomic) ADVCellConfigurationBlock configurationBlock;
@end
@implementation ADVArrayDataSource
{
NSMutableArray *_array;
}
#pragma mark - ADVArrayDataSource
- (instancetype) initWithArray:(NSArray *)array
reusableCellIdentifier:(NSString *)identifier
cellConfigurationBlock:(ADVCellConfigurationBlock)block
{
if ((self = [super init]))
{
_configurationBlock = block;
_cellIdentifier = identifier;
_array = [array mutableCopy];
}
return self;
}
- (NSInteger) numberOfItemsInSection:(NSInteger)section
{
return self.array.count;
}
- (void) deleteObjectAtIndexPath:(NSIndexPath *)indexPath
{
[_array removeObjectAtIndex:indexPath.row];
}
#pragma mark - ADVIndexedSubscripting
- (id) objectAtIndexedSubscript:(NSUInteger)index
{
return self.array[index];
}
#pragma mark - UITableViewDataSource
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self numberOfItemsInSection:section];
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
self.configurationBlock(cell, self[indexPath.row]);
return cell;
}
- (void) tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (editingStyle)
{
case UITableViewCellEditingStyleDelete:
[self deleteObjectAtIndexPath:indexPath];
break;
default:
break;
}
}
#pragma mark - UICollectionViewDataSource
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [self numberOfItemsInSection:section];
}
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:self.cellIdentifier
forIndexPath:indexPath];
self.configurationBlock(cell, self[indexPath.row]);
return cell;
}
@end
//
// Copyright © 2013 Yuri Kotov
//
#import <Foundation/Foundation.h>
@protocol ADVIndexedSubscripting <NSObject>
- (id) objectAtIndexedSubscript:(NSUInteger)index;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment