ZXTableAutoHeight
基于单元格的自动布局的更有效的 UITableViewCell Self-Sizing 框架。
多语言翻译
主要功能
- 根据单元格的自动布局自动计算高度并缓存结果
- 仅更新由 Heckel 差分算法分析的数据更改部分
- 在单元格符合特定规范后自动传递数据到单元格
- 可以管理大量的 tableView
需求
- iOS 11.0+
安装
CocoaPods
首选的安装方法是使用 CocoaPods。将以下内容添加到您的 Podfile
pod 'ZXTableAutoHeight'
手动安装
将 AutoHeightManger 目录复制到您的项目中
开始使用
步骤 1
- UITableViewCell 遵守协议
#import <ZXTableAutoHeight/ZXTableAutoHeight.h>
// .h
@interface FDCustomTableViewCell : UITableViewCell<ZXTableViewAutoHeightCellProtocol>
@end
// .m
@implementation FDCustomTableViewCell
- (void)zx_updateUIWithModel:(id)model {
// update UI based on model
...
}
@end
步骤 2
- 实现代理和数据源
#import <ZXTableAutoHeight/ZXTableAutoHeight.h>
@interface MainViewController ()<ZXTableViewAutoHeightDelegate,ZXTableViewAutoHeightDataSource>
@property (nonatomic, strong) UITableView * listView;
@property (nonatomic, strong) NSMutableArray * dataList;
@property (nonatomic, strong) ZXTableViewAutoHeightManager * autoManager;
@end
@implementation MainViewController
#pragma mark - Life Circle
- (void)viewDidLoad {
[super viewDidLoad];
...
// register tableView to manager
[self.autoManager registerTableView:self.listView];
}
#pragma mark - ZXTableViewAutoHeightDataSource
// section number
- (NSInteger)zx_numberOfSectionsInTableView:(UITableView *)tableView {
return self.dataList.count;
}
// section items
- (NSArray *_Nullable)zx_tableView:(UITableView *_Nullable)tableView objectsInSection:(NSInteger)section {
NSArray * list = self.dataList[section];
return list;
}
// cell Class
- (Class)zx_tableView:(UITableView *)tableView cellClassForRowAtIndexPath:(NSIndexPath *)indexPath object:(id)object {
return [FDCustomTableViewCell class];
}
#pragma mark - ZXTableViewAutoHeightDelegate
// cope cell after generate ( like event cope )
- (void)zx_tableView:(UITableView *)tableView didGenerateCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath object:(id)object {
...
}
@end
步骤 3
- 更新修改部分(在仅更改数据值时应标记数据)
#import <ZXTableAutoHeight/ZXTableAutoHeight.h>
- (void)modifyItem:(NSIndexPath *)indexPath {
NSMutableArray * obj = self.dataList[indexPath.section];
ZXCustomCellModel * m = obj[indexPath.row];
m.index = 3;
// Mark Previous Data as Modified
[m zx_markModify];
// update modified part only
[self.autoManager updateTableView:tableView animation:UITableViewRowAnimationNone completion:nil];
}
- (void)insertItem:(NSIndexPath *)indexPath {
NSMutableArray * obj = self.dataList[indexPath.section];
ZXCustomCellModel * m1 = [ZXCustomCellModel new];
// insert/delete/replace data without mark
[obj insertObject:m1 atIndex:indexPath.row];
// update changed part only
[self.autoManager updateTableView:tableView animation:UITableViewRowAnimationNone completion:nil];
}
协议
ZXTableAutoHeight
采用 MIT 协议。