Nimbus有关表格数据的模块化源码阅读

nimbuskit这个三方其实很大,东西非常多
比如支持超链接的label 、web view 组件 、标准化的程序通信 、强大的debug工具 等等。
因为项目需求,所以看了一遍它关于它模块化组装tableview的源码。。做个笔记记录
它的结构其实类似于早期我们cell部分cellModel的封装,进一步的封装了cell形成,只不过它做的封装更为完善一些,但是也有一些不足
源码部分分为了几大块,表格的代理 NITableViewActions,数据源 NITableViewModel
下面看看具体的核心类目,其余常规类目暂时不看
数据代理类目:
@interface NIObjectActions : NSObject
@property (nonatomic, copy) NIActionBlock tapAction;
@property (nonatomic, copy) NIActionBlock detailAction;
@property (nonatomic, copy) NIActionBlock navigateAction;
@property (nonatomic) SEL tapSelector;
@property (nonatomic) SEL detailSelector;
@property (nonatomic) SEL navigateSelector;
@end

NIActions : NSObject
@property (nonatomic, strong) NSMutableDictionary* objectToAction;<object.hash,NIObjectActions>
@property (nonatomic, strong) NSMutableDictionary* classToAction;<class,NIObjectActions>
@property (nonatomic, strong) NSMutableSet* objectSet; <NIObjectActions>

  • (id)attachToObject:(id<NSObject>)object tapBlock:(NIActionBlock)action;
    。。。
  • (id)attachToObject:(id<NSObject>)object tapSelector:(SEL)selector;
    。。。

pragma mark Mapping Classes

  • (void)attachToClass:(Class)aClass tapBlock:(NIActionBlock)action;
    。。。
  • (void)attachToClass:(Class)aClass tapSelector:(SEL)selector;
    。。。。

主代理类目期内实现了table的代理方法,通过NIActions定义了基本的行为方式监听
NITableViewActions : NIActions <UITableViewDelegate>
NSMutableSet* forwardDelegates;

数据源类目:
section独立单位
@interface NITableViewModelSection : NSObject

  • (id)section;
    @property (nonatomic, copy) NSString* headerTitle;
    @property (nonatomic, copy) NSString* footerTitle;
    @property (nonatomic, strong) NSArray* rows; —> string/NITableViewModelFooter/cusdefineObject

@end

主数据源类目
@interface NITableViewModel : NSObject <NIActionsDataSource, UITableViewDataSource>

( NIActionsDataSource :- (id)objectAtIndexPath:(NSIndexPath *)indexPath; )

@property (nonatomic, strong) NSArray* sections; <NITableViewModelSection> // Array of NITableViewModelSection
@property (nonatomic, strong) NSArray* sectionIndexTitles;
@property (nonatomic, strong) NSDictionary* sectionPrefixToSectionIndex;

pragma mark Creating Table View Cells

@property (nonatomic, weak) id<NITableViewModelDelegate> delegate;

if NS_BLOCKS_AVAILABLE

// If both the delegate and this block are provided, cells returned by this block will be used
// and the delegate will not be called.
@property (nonatomic, copy) NITableViewModelCellForIndexPathBlock createCellBlock;
//UITableViewDataSource

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
  • (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
    。。。。

//block和代理选用其一。。。。

  • (UITableViewCell *)tableView: (UITableView *)tableView
    cellForRowAtIndexPath: (NSIndexPath *)indexPath {
    id object = [self objectAtIndexPath:indexPath];

    UITableViewCell* cell = nil;

if NS_BLOCKS_AVAILABLE

if (nil != self.createCellBlock) {
cell = self.createCellBlock(tableView, indexPath, object);
}

endif

if (nil == cell) {
cell = [self.delegate tableViewModel:self
cellForTableView:tableView
atIndexPath:indexPath
withObject:object];
}

return cell;
}

主要的cell协议和基本数据类型
@protocol NICellObject <NSObject>
@required

  • (Class)cellClass;
  • (UITableViewCellStyle)cellStyle;
    @protocol NINibCellObject <NSObject>
    @required
  • (UINib *)cellNib;
  • (Class)nibCellClass;

@interface NICellObject : NSObject <NICellObject>
// Designated initializer.

  • (id)initWithCellClass:(Class)cellClass userInfo:(id)userInfo;
  • (id)initWithCellClass:(Class)cellClass;
  • (id)objectWithCellClass:(Class)cellClass userInfo:(id)userInfo;
  • (id)objectWithCellClass:(Class)cellClass;
    @property (nonatomic, strong) id userInfo;

@protocol NICell <NSObject>
-shouldUpdateCellWithObject
+shouldAppendObjectClassToReuseIdentifier

  • (CGFloat)heightForObject:(id)object atIndexPath:(NSIndexPath *)indexPath tableView:

主cell工厂类目。它负责cell的产生。并兼顾刷新数据
@interface NICellFactory : NSObject <NITableViewModelDelegate>

NSMutableDictionary* objectToCellMap;

  • (UITableViewCell *)tableViewModel:(NITableViewModel *)tableViewModel cellForTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath withObject:(id)object;
  • (void)mapObjectClass:(Class)objectClass toCellClass:(Class)cellClass;

  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath model:(NITableViewModel *)model;

  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath model:(NITableViewModel *)model;

其主要流程是这样的。
定义代理action主体,通过设置代理类目NITableViewActions代理tableview的具体事件,同时指定本身为代理对象。
[[NITableViewActions alloc] initWithTarget:self]
可以覆盖实现。能够使封装和自定义不冲突。

期间行为关联是通过NIActions内部attach属性,存入了相关的字典、set内部,进行了捆绑操作。
其实这里也有些不足。基本细化控件的内部操作是个短板总觉得。。
数据源的定义是通过NITableViewModel初始化了NITableViewModelSection数据组信息,来进行相应的展示。
其中string默认设置
同时,它捆绑定义了NITableViewModelDelegate协议,
并提供了NICellFactory类辅工厂来辅助实现产生具体cell
产生

  • (id)initWithCellClass:(Class)cellClass userInfo:(id)userInfo;
    同时具体cell信息遵循NICell协议。来实现数据的更新操作
    (id<NICell>)cell shouldUpdateCellWithObject:object

table.DataSource = NITableViewModel(初始化dataarray的sections:@[ NITableViewModelSection:header、footer、rows<cellObject> ] ,加载cell数据的delegate:NICellFactory< NITableViewModelDelegate>)

整体流程还算很简单吧。。因为具体控件的点击是它本身短板,所以使用的时候,这方面最好在它基础上做了一层封装,使开发更加的便捷简单。

cell高度自定义成了这样。方便在model中增加rowHeight变量,更方便获取cell高度 或者直接扩展ActionModel,但是此种方法需要获取内部变量forwarDelegate。有些危险。所以放弃了。。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height = tableView.rowHeight;
id object = [(NITableViewModel )tableView.dataSource objectAtIndexPath:indexPath];
if (object && [object isKindOfClass:NICellObject.class]) {
if ([object respondsToSelector:@selector(rowHeight)]) {
SEL selector = NSSelectorFromString(@"rowHeight");
IMP imp = [object methodForSelector:selector];
float (
func)(id , SEL) = (void *)imp;
float result = func(object , selector);
if (result != 0) {
return result;
}
}
}
id class = [object cellClass];
if ([class respondsToSelector:@selector(heightForObject:atIndexPath:tableView:)]) {
height = [class heightForObject:object atIndexPath:indexPath tableView:tableView];
}
return height;
}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,546评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,224评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,911评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,737评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,753评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,598评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,338评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,249评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,696评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,888评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,013评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,731评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,348评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,929评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,048评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,203评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,960评论 2 355

推荐阅读更多精彩内容