Skip to content

Instantly share code, notes, and snippets.

@hsavit1
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hsavit1/b14a4dba86c0dd814aac to your computer and use it in GitHub Desktop.
Save hsavit1/b14a4dba86c0dd814aac to your computer and use it in GitHub Desktop.
MVVMTableViewListDataSource
@import Foundation;
@import UIKit;
typedef UITableViewCell *(^MVVMCellCreatorBlock)(UITableView * tableView, NSIndexPath * indexPath, id object);
@interface MVVMTableViewListDataSource : NSObject <UITableViewDataSource>
- (id)initWithObjects:(NSArray *)objects cellCreator:(MVVMCellCreatorBlock)block;
@property (nonatomic, copy) NSArray * objects;
@property (nonatomic, strong, readonly) RACSignal * objectsUpdateSignal;
@end
//////////////////////////////////
#import "MVVMTableViewListDataSource.h"
@interface MVVMTableViewListDataSource ()
@property (nonatomic, strong, readwrite) RACSignal * objectsUpdateSignal;
@property (nonatomic, copy) MVVMCellCreatorBlock cellCreator;
@end
@implementation MVVMTableViewListDataSource
- (id)initWithObjects:(NSArray *)objects cellCreator:(MVVMCellCreatorBlock)block {
NSParameterAssert(block);
if ((self = [super init])) {
_cellCreator = block;
_objects = objects ?: @[];
_objectsUpdateSignal = RACObserve(self, objects);
}
return self;
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.objects count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSAssert(self.cellCreator, @"No cell creator.");
return self.cellCreator(tableView, indexPath, self.objects[indexPath.row]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment