Last active
August 29, 2015 14:23
-
-
Save hsavit1/b14a4dba86c0dd814aac to your computer and use it in GitHub Desktop.
MVVMTableViewListDataSource
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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