ABDataSourceController 1.1.0

ABDataSourceController 1.1.0

测试已测试
语言语言 Obj-CObjective C
许可协议 MIT
发布日期最后发布2016年6月

Alex Bumbu 维护。



  • Alex Bumbu

为从 UIViewController 中删除 UITableViewDataSource 和 UICollectionViewDataSource 代码,提供一个简单的 Objective-C 和 Swift 实现,并在 UIViewControllers 之间共享。

兼容性

  • iOS 7.0 或更高版本

集成

  • 通过 Cocoapods 安装(只需将 pod 'ABDataSourceController', '~> 1.1' 添加到 Podfile),或克隆此仓库并将 ABDataSourceController/objc 文件夹的内容拖放到项目中。
  • 要用于表格视图,只需创建实施 ABTableViewDataSourceController 协议的自定义数据源控制器对象。
#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
  • 现在重写 refreshDataSourceWithCompletionHandler 方法来加载数据源
- (void)refreshDataSourceWithCompletionHandler:(void (^)())completion {
    [self loadDataSource];

    if (completion)
        completion();
}
  • 并编写所有的 UITableViewDataSourceUITableViewDelegate 代码
#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;
}
  • 要从 Interface Builder 使用,向您的视图控制器的 XIB 添加对象,并将其类更改为 CustomDataSourceController(《点击此处获取教程》)。现在将新创建的对象的 tableViewviewController 端口连接,并将表视图的 dataSourceController 端口连接到您的自定义对象。您就绪了!
  • 要从代码中初始化,请将数据源控制器初始化到您的 UIViewController 中,将其分配给您的 UITableView 的 dataSourceController 属性,您就绪了!
#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
  • 要用于集合视图,按照上面描述的相同原则操作,并确保您的自定义数据源控制器对象符合 ABCollectionViewDataSourceController 协议。

沟通

  • 如果您 找到了一个错误,请打开一个问题。
  • 如果您 有功能请求,请打开一个问题。
  • 如果您 希望做出贡献,请提交一个拉取请求。

致谢

ABDataSourceController 是由 Alex Bumbu 创建的。

许可协议

ABDataSourceController 可在 MIT 许可证下使用。有关更多信息,请参阅 LICENSE 文件。如果您不想注明出处,请联系 Alex Bumbu