Home

Awesome

ABDataSourceController

Simple Objective-C and Swift implementation for getting rid of your UITableView & UICollectionView delegate and data source code from your UIViewController and share it between UIViewControllers.

Compatibility

Integration

#import "ABDataSourceController.h"

@interface CustomDataSourceController : NSObject <ABTableViewDataSourceController>

@property (nonatomic, assign) IBOutlet UITableView *tableView;
@property (nonatomic, assign) IBOutlet UIViewController *viewController;

@end

@implementation CustomDataSourceController {
    NSArray *_dataSource;
}

@synthesize tableView = _tableView;
@synthesize viewController = _viewController;

@end
- (void)refreshDataSourceWithCompletionHandler:(void (^)())completion {
    [self loadDataSource];

    if (completion)
        completion();
}
#pragma mark UITableViewDataSource Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSource.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"DefaultCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }

    cell.textLabel.text = [_dataSource objectAtIndex:indexPath.row];

    return cell;
}

#import "UITableView+DataSourceController.h"

@implementation MainViewController {
    IBOutlet UITableView *tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    // setup data source controller
    CustomDataSourceController *dataSourceCtrl = [[CustomDataSourceController alloc] init];
    dataSourceCtrl.tableView = tableView;
    dataSourceCtrl.viewController = self;
    tableView.dataSourceController = dataSourceCtrl;

    // load data source
    [tableView.dataSourceController refreshDataSourceWithCompletionHandler:nil];
}

@end

Communication

Credits

ABDataSourceController was created by Alex Bumbu.

License

ABDataSourceController is available under the MIT license. See the LICENSE file for more info. For usage without attribution contact Alex Bumbu.