瘦身UITableViewController

作者: piggybear | 来源:发表于2016-11-26 13:47 被阅读134次

如果我们用UITableViewController的话,一般情况下都会重复的在每个类里面写如下三个方法

#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PGTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellReuseIdentifier];
    return cell;
}

如果项目大的话,每个类都要写,写的很臃肿,很麻烦,那么有没有办法可以解决呢
答案就是PGBaseDataSource

PGBaseDataSource的写法

[[PGBaseDataSource instance] numberOfSectionsInTableView:^NSUInteger(UITableView *tableView) {
    return 1;
} numberOfRowsInSection:^NSUInteger(UITableView *tableView, NSInteger section) {
    return 10;
} cellForRowAtIndexPath:^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
    PGTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellReuseIdentifier];
    return cell;
}];

Cocoapods安装

pod 'PGBaseDataSource'

PGBaseDataSource地址

https://github.com/xiaozhuxiong121/PGBaseDataSource

相关文章

网友评论

    本文标题:瘦身UITableViewController

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