iOS 从UITableViewController中分离数据源

之前看objc.io #1 Light View Controllers看到一个很不错的技巧:从UITableViewController中分离数据源,这样可以减小UITableViewController的规模,同时也能让程序有一个比较好的架构。

由于UITableViewController是iOS中使用得最频繁的一个视图控制器,所以这里做下笔记,记录下这个技巧。

首先是故事板(当然也可以用代码 + XIB的组合):



新建一个Cell类,连接故事板中的Outlets,代码如下:

#import <UIKit/UIKit.h>

@interface Cell : UITableViewCell

- (void)configureForData:(NSString *)data;

@end

#import "Cell.h"

@interface Cell ()

@property (weak, nonatomic) IBOutlet UILabel *dataTitleLabel;

@property (weak, nonatomic) IBOutlet UIButton *dataDetailLabel;

@end

@implementation Cell

- (void)configureForData:(NSString *)data {
    self.dataTitleLabel.text = data;
    [self.dataDetailLabel setTitle:@"1" forState:UIControlStateNormal];
}


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

Cell类中的configureForData方法用于配置Cell中UI的内容。


回到TableViewController类,代码如下:

#import "TableViewController.h"
#import "DataSource.h"
#import "Cell.h"

@interface TableViewController ()

@property (strong, nonatomic) NSArray *array;
@property (strong, nonatomic) DataSource *dataSource;

@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.array = @[@"1", @"2", @"3", @"1", @"2", @"3", @"1", @"2", @"3"];
    
    [self setupTableView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/* 设置表格的数据源,并registerNib */
- (void)setupTableView {
    
    TableViewCellConfigureBlock configureCell = ^(Cell *cell, NSString *str) {
        [cell configureForData:str];
    };
    self.dataSource = [[DataSource alloc] initWithItems:_array
                                                         cellIdentifier:@"Cell"
                                                     configureCellBlock:configureCell];
    self.tableView.dataSource = self.dataSource;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100.0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@", self.array[indexPath.row]);
}

@end

其中setupTableView方法将表格的UITableViewDataSource “外包”给TableDataSource类实现。

本类实现UITableViewDelegate,包括点击表格中的某一行的行为,cell的高度等。


最后看看承担表格数据源责任的TableViewDataSource类:

#import <Foundation/Foundation.h>

typedef void (^TableViewCellConfigureBlock)(id cell, id item);

@interface DataSource : NSObject <UITableViewDataSource>

- (id)initWithItems:(NSArray *)anItems
     cellIdentifier:(NSString *)aCellIdentifier
 configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock;

- (id)itemAtIndexPath:(NSIndexPath *)indexPath;

@end

首先该类必须遵守UITableViewDataSource委托,然后定义一个配置Cell的Block类型。

该类的实现代码如下:

#import "DataSource.h"

@interface DataSource ()

@property (nonatomic, strong) NSArray  *items;
@property (nonatomic, copy)   NSString *cellIdentifier;
@property (nonatomic, copy)   TableViewCellConfigureBlock configureCellBlock;

@end

@implementation DataSource

#pragma mark - Initialization

- (id)init {
    // 只能通过initWithItems:cellIdentifier:configureCellBlock:方法初始化
    return nil;
}

- (id)initWithItems:(NSArray *)anItems
     cellIdentifier:(NSString *)aCellIdentifier
 configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock
{
    self = [super init];
    
    if (self) {
        self.items              = anItems;
        self.cellIdentifier     = aCellIdentifier;
        self.configureCellBlock = [aConfigureCellBlock copy];
    }
    
    return self;
}

- (id)itemAtIndexPath:(NSIndexPath *)indexPath {
    return self.items[(NSUInteger) indexPath.row];
}

#pragma mark UITableViewDataSource

/* Required methods */

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier
                                                            forIndexPath:indexPath];
    id item = [self itemAtIndexPath:indexPath];
    
    /**
     之所以把configureCellBlock作为一个属性,是为了该类可以被复用
     只要TableViewController定制了对应的代码块并作为参数传入就可以了
     
     复用的关键:不要被具体的实现代码入侵,只需要调用接口和给出接口就可以了
     */
    self.configureCellBlock(cell, item);
    return cell;
}

@end

说下cellForRowAtIndexPath方法中的self.configureCellBlock(cell, item);

这句代码的作用无疑是配置Cell中的内容,一般由用户自定义的Cell类自行实现,这里没有牵涉任何实现细节,从而保证TableViewDataSource类可以很好地被复用。


运行结果:


顺便传了个Demo上来,有兴趣的可以下载看看。


参考资料:Lighter View Controllers



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是一个iOS项目实例,其使用了UITableViewUITableViewController控件来展示列表数据: 项目名称:美食杂谈 项目简介:该项目是一个美食分享社区,用户可以在其分享自己喜欢的美食,查看其他用户的分享,以及评论和点赞。 视图1:登录页面 - 展示登录表单,包括账号和密码输入框,以及登录按钮 - 支持新用户注册和忘记密码操作 视图2:首页 - 轮播图展示美食图片和热门话题 - 美食分享列表,展示最新的美食分享 - 入口按钮,包括发布分享、查看个人心、搜索等入口 视图3:美食分享详情页 - 展示美食分享的图片、标题、描述、点赞数和评论列表 - 支持点赞和评论操作 视图4:发布分享页面 - 展示发布表单,包括美食图片、标题、描述等字段 - 支持选择美食分类和添加标签 - 支持上传图片和发布分享操作 视图5:个人心 - 展示个人信息和发布的美食分享列表 - 支持修改个人信息和注销操作 - 支持编辑和删除自己发布的分享 视图6:美食分类列表 - 展示不同的美食分类和热门标签 - 支持选择分类和标签查看相关分享 视图7:搜索页面 - 展示搜索框和搜索结果列表 - 支持根据关键词搜索美食分享、分类、标签等内容 在以上的设计,可以使用一个UITableViewController来展示美食分享列表、分类列表和搜索结果列表;可以在美食分享详情页和个人使用UITableView来展示评论列表和美食分享列表。通过UITableViewUITableViewController控件,可以方便地展示列表数据和处理相关事件,提高开发效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值