美文网首页
OC:抽出数据源代理

OC:抽出数据源代理

作者: 春暖花已开 | 来源:发表于2019-04-07 00:38 被阅读0次

前言: 为控制器瘦身,本文提供一个抽出数据源代理的示例,具体请结合自身的需求定制。抽出数据源代理不是必须的,如果控制器不臃肿就没必要了,因为抽出数据源代理会增加类,而且还可能复杂化工程。

MZTableViewDataSource.h

#import <UIKit/UIKit.h>

@interface MZTableViewDataSource : NSObject <UITableViewDataSource>

/** 配置第一次加载的数据,设置复用标记 */
- (instancetype)initWithOriginalList:(NSArray *)list cellIdentifier:(NSString *)cellIdentifier;

/** 当加载更多 或 刷新数据的时候用 */
- (void)reloadDataSourceWithArray:(NSArray *)array;
@end

MZTableViewDataSource.m

#import "MZTableViewDataSource.h"

//Views
#import "MZTableViewCell.h"

@interface MZTableViewDataSource ()

@property (nonatomic, strong) NSArray *dataList;
@property (nonatomic, copy) NSString *cellIdentifier;

@end

@implementation MZTableViewDataSource

/** 配置第一次加载的数据,设置复用标记 */
- (instancetype)initWithOriginalList:(NSArray *)list cellIdentifier:(NSString *)cellIdentifier {
    
    if (self = [super init]) {
        self.dataList = list;
        self.cellIdentifier = cellIdentifier;
    }
    return self;
}

/** 当加载更多 或 刷新数据的时候用 */
- (void)reloadDataSourceWithArray:(NSArray *)array {
    self.dataList = array;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MZTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier];
    cell.model = self.dataList[indexPath.row];
    return cell;
}

@end

ViewController.m

#import "ViewController.h"

#import "DataModel.h"
#import "MZTableViewDataSource.h"

#import "MZTableViewCell.h"

@interface ViewController ()<UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *mTableView;

@property (nonatomic, strong) MZTableViewDataSource *dataSource;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.mTableView registerNib:[UINib nibWithNibName:NSStringFromClass([MZTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([MZTableViewCell class])];
    self.mTableView.rowHeight = UITableViewAutomaticDimension;
    
    self.dataSource = [[MZTableViewDataSource alloc] initWithOriginalList:[self loadDataFromPlist] cellIdentifier:NSStringFromClass([MZTableViewCell class])];
    self.mTableView.dataSource = self.dataSource;
}

- (NSArray *)loadDataFromPlist {
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"DATA.plist" ofType:nil]];
    NSMutableArray *result = [NSMutableArray array];
    
    for (NSDictionary *object in dict[@"data"]) {
        [result addObject:[[DataModel alloc] initWithDictionary:object]];
    }
    return result;
}

#pragma mark - delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
@end

相关文章

  • OC:抽出数据源代理

    前言: 为控制器瘦身,本文提供一个抽出数据源代理的示例,具体请结合自身的需求定制。抽出数据源代理不是必须的,如果控...

  • OC、swift混编中的反向传值

    一 OC向swift传值 1) 代理 1.1在oc中创建 代理 #import @protocolSecon...

  • UITableView

    UITableView的数据源和代理: 数据源方法 必须要实现的数据源方法- (NSInteger)tableVi...

  • UIPickerView

    数据源方法 代理方法 普通方法

  • 3. UIPickerview 和 UIDatapicker 的

    UIPickerView的基本使用 1.遵守数据源协议/代理协议 2.实现数据源方法 3.其他常用代理方法 UID...

  • TableView 的使用《1》!

    首先: -- 代理 和数据源!

  • UITableView

    UITableView的数据源和代理: 数据源方法 必须要实现的数据源方法 (1 ,2 必须实现) 1. - (...

  • iOS delegate为什么用weak,而不用strong/a

    什么是代理? iOS开发,OC是常用开发语言,但是OC仅仅是单继承,但是我们可以用代理来替代,那么什么是代理呢? ...

  • UICollectionView 基本使用

    控制器继承UICollectionViewController 它自动遵守数据源 和 代理了。 1.实现数据源方法...

  • iOS tableView详解

    1、UITableViewDataSource数据源方法 2、UITableViewDelegate代理方法 3、...

网友评论

      本文标题:OC:抽出数据源代理

      本文链接:https://www.haomeiwen.com/subject/recjiqtx.html