ZNTableView - 对UITableview的逻辑封装

ZNTableViewKit的使用(GitHub地址:https://github.com/dreamfly-nan/ZNUIKit/tree/master/ZNUIKit/ZNUIKit/Public/ZNTableViewKit

基于UITableview有着共通性,也是因为懒和逻辑解耦优化,我们理想的认为数据管理数据,视图管理视图,事件管理管理,单一职能原则,三者互不交叉,以此来降低代码的逻辑复杂度,同时又使得代码具备统一性,方便阅读,有利于团队协作。在这个理念的基础上,封装了ZNTableView组件,通过ZNTableviewKit做了隔断,解耦三者关系。

ZNTableViewKit

ZNTableViewKit

下面来看下主要的几个协议和对象的使用:

ZNTableViewHelper,UITableVIew的视图管理对象

ZNTableViewHelper实现两个协议,分别是ZNTableViewLunchProtocol(加载协议),ZNTableViewLayoutProtocol(布局协议)

ZNTableViewLunchProtocol加载协议

/// 加载过程

@protocol ZNTableViewLunchProtocol <NSObject>

@optional

/// 根据indexPath返回对应的cell的class

/// 默认取第一个注册的cell

/// @param indexPath <#indexPath description#>

/// @param model <#model description#>

- (Class)cellClassWithIndexPath:(NSIndexPath *) indexPath

                          model:(id) model;

/// 加载

/// @param cell <#cell description#>

/// @param indexPath <#indexPath description#>

- (void)lunchTableViewCell:(UITableViewCell *) cell

                indexPath:(NSIndexPath *) indexPath;

/// cell即将出现时调用

/// @param cell <#cell description#>

/// @param indexPath <#indexPath description#>

- (void)willDisplayWithTableViewCell:(UITableViewCell *) cell

                          indexPath:(NSIndexPath *) indexPath;

@end

ZNTableViewLayoutProtocol布局协议

/// 布局

@protocol ZNTableViewLayoutProtocol <NSObject>

@optional

/// 每一项的高度,优先使用这边回掉的高度值,没有实现则直接取注册的高度值,

/// 没设置注册的高度值,则直接使用默认高度值

/// @param indexPath <#indexPath description#>

/// @param model <#model description#>

- (CGFloat)rowHeightWithIndexPath:(NSIndexPath *) indexPath

                        tableView:(UITableView *) tableView

                            model:(id) model;

/// 组头的高度

/// @param section <#section description#>

- (CGFloat)headHeightWithSection:(NSInteger) section;

/// 组头

/// @param section <#section description#>

- (UIView<ZNBaseViewProtocol> * __nullable)headViewWithSection:(NSInteger) section;

/// 组尾的高度

/// @param section <#section description#>

- (CGFloat)footerHeightWithSection:(NSInteger) section;

/// 组尾

/// @param section <#section description#>

- (UIView<ZNBaseViewProtocol> * __nullable)footerViewWithSection:(NSInteger) section;

@end

ZNTableViewDataLoader,UITableView的数据源管理对象

ZNTableViewDataLoader实现协议ZNTableViewDataSourceProtocol(数据管理),基本形态的数据管理已经在ZNTableViewDataLoader内部进行实现,使用者如果有特殊的需求,可继承该对象进行重写或者添加新方法。而对象中的loadFinishBlock代码块是根据数据源中数据处理结束后进行调用,而组件对其调用的传参进行解析并展示对应的视图样式,例如空视图,或者错误视图之类的。

这边有一个注意点:'- (void)loadData:(BOOL) isReSetData;'只有该方法会触发加载视图,而加载视图的消失,是在调用loadFinishBlock后消失的,具体的使用还需要自己去实验下看看。

- (void)loadData:(BOOL) isReSetData;

ZNTableViewDataSourceProtocol数据管理协议

@protocol ZNTableViewDataSourceProtocol <NSObject>

/// 加载完成后回调

@property(nonatomic, copy,nullable) ZNLoadFinishBlock loadFinishBlock;

/// 重设数据源

/// @param array <#array description#>

- (void)setDataSourceWithArray:(id) array;

/// 获取数据源

- (id)obtainDataSource;

/// 是否有数据,未实现则默认无数据

- (BOOL)haveData;

/// 是否是下拉刷新数据

/// @param isReSetData <#isReSetData description#>

- (void)loadData:(BOOL)isReSetData;

/// 组数 - 未实现则默认返回0

- (NSInteger)numberOfSection;

/// 每组的row数量

/// @param section <#section description#>

- (NSInteger)numbserOfRowWithSecion:(NSInteger)section;

/// 根据indexPath获取对应的数据模型

/// @param indexPath <#indexPath description#>

- (NSObject *)obtianObjectWithIndexPath:(NSIndexPath *)indexPath;

/// 获取该组的数据数组

/// @param section <#section description#>

- (NSArray *)obtainArrayWithSecion:(NSInteger)section;

@optional

/// 是否是头部

/// @param indexPath <#indexPath description#>

- (BOOL)isHeaderWithIndexPath:(NSIndexPath *) indexPath;

/// 是否是尾部

/// @param indexPath <#indexPath description#>

- (BOOL)isFooterWithIndexPath:(NSIndexPath *) indexPath;

@end

ZNTableViewKit,UITableView的管理对象

使用ZNTableViewKit进行注册对应的Cell视图,空视图,错误视图,以及加载视图,头部,尾部刷新等;同时可注册数据策略,操作事件;以及加载调用。

@interface ZNTableViewKit : NSObject <ZNTableViewKitProtocol>

@property(nonatomic , weak) id<ZNTableViewKitDelegate> ZNDelegate;

/// 只传入数据源与tableView

/// @param tableView <#tableView description#>

/// @param dataSource <#dataSource description#>

- (instancetype)initWithSingleGroupTableView:(UITableView *) tableView

                                  dataSource:(NSArray *) dataSource;

/// 只传入数据源与tableView

/// @param tableView <#tableView description#>

/// @param dataSource <#dataSource description#>

- (instancetype)initWithMoreGroupTableView:(UITableView *) tableView

                                dataSource:(NSArray *) dataSource;

/// 不用传manager对象的实例化方法

/// @param viewHelper <#viewHelper description#>

/// @param dataLoader <#dataLoader description#>

/// @param tableView <#tableView description#>

- (instancetype)initWithViewHelper:(id<ZNTableViewLayoutProtocol,

                                    ZNTableViewLunchProtocol> ) viewHelper

                        dataLoader:(id<ZNTableViewDataSourceProtocol>) dataLoader

                        tableView:(UITableView *) tableView;

/// 当前tableview的处理对象

/// @param tableView 主tableview

/// @param manager 管理对象

- (instancetype)initWithTableView:(UITableView *) tableView

                          manager:(ZNTableViewManage *) manager;

/// 注册对应的cell

/// @param model <#model description#>

- (void)addRegisterModel:(ZNRegisterModel *) model;

/// 注册对应的cell

/// @param models <#models description#>

- (void)addRegisterModels:(NSArray<ZNRegisterModel *> *) models;

/// 添加策略

/// @param strategyClass <#strategyClass description#>

- (void)addRegisterStrategyWithClass:(id<ZNTableViewStrategyProtocol>) strategyClass;

/// 添加事件

/// @param action <#action description#>

- (void)addAction:(id<ZNTableViewActionProtocol>) action;

/// 空视图接入

/// @param emptyView <#emptyView description#>=

- (void)setEmptyView:(UIView*) emptyView;

/// 错误视图接入

/// @param errorView <#errorView description#>

- (void)setErrorView:(UIView *) errorView;

/// 加载图接入

/// @param loadView <#loadView description#>

- (void)setLoadView:(UIView *) loadView;

/// 注册头部

/// @param header <#header description#>

- (void)registerRefreshHead:(MJRefreshHeader *) header;

/// 注册尾部

/// @param footer <#footer description#>

- (void)registerRefreshFoot:(MJRefreshFooter *) footer;

- (void)loadData:(BOOL) isReSetData;

@end

NRegisterModel注册cell模型对象

该对象主要包含Cell的类型,高度,已经对应的数据类型名称,ZNTableViewKit会根据用户注册的模型进行自动匹配对应的cell,高度,以及给cell设置对应的数据模型。

@interface ZNRegisterModel : NSObject

@property(nonatomic , strong, readonly) Class cellClass;

///模型名称

@property(nonatomic , strong, readonly) NSString * modelName;

@property(nonatomic , assign) CGFloat height;

@property(nonatomic , assign ,readonly) BOOL cutsHeight;

/// 注册cell

/// @param cellClass <#cellClass description#>

+ (instancetype)initWithCellClass:(Class) cellClass;

/// 注册cell

/// @param cellClass <#cellClass description#>

/// @param height <#height description#>

+ (instancetype)initWithCellClass:(Class) cellClass

                          height:(CGFloat) height;

/// 注册cell,并且注册cell对应的model对象

/// @param cellClass <#cellClass description#>

/// @param modelName <#modelName description#>

+ (instancetype)initWithCellClass:(Class)cellClass

                        modelName:(NSString*) modelName;

/// 注册cell,并且注册cell对应的model对象

/// @param cellClass <#cellClass description#>

/// @param modelName <#modelName description#>

/// @param height <#height description#>

+ (instancetype)initWithCellClass:(Class)cellClass

                        modelName:(NSString*) modelName

                          height:(CGFloat) height;

+ (NSArray *)initWithArrayCellClass:(NSArray<Class> *) arrayClass;

@end

注意

如果存在一种数据模型对应多种cell,则需要自行重写ZNTableViewHelper对象以下方法

/// 根据indexPath返回对应的cell的class

/// 默认取第一个注册的cell

/// @param indexPath <#indexPath description#>

/// @param model <#model description#>

- (Class)cellClassWithIndexPath:(NSIndexPath *) indexPath

                          model:(id) model;

Cell对象需实现ZNBaseTableViewCellProtocol协议

组头组尾如有重写需实现ZNBaseViewProtocol协议

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容