HoloTableView 3.1.2

HoloTableView 3.1.2

gonghonglou 维护。



  • gonghonglou

HoloTableView

CI Status Version License Platform

HoloTableView 内部封装了 UITableView 的 delegate 方法,提供了一个链式语法调用。将这些 UITableView 的 delegate 方法分布到每个 cell 上,每个 cell 都有设置 Class、模型、高度和点击事件等方法。

功能

  • 提供分区和行创建器来处理 UITableVIew 的代理事件。
  • 提供在单元格、标头和页脚中实现的协议,以处理 UITableVIew 的代理事件。
  • cell 提供左滑和右滑操作。
  • 通过响应者链传递事件。
  • 支持为行、标头和页脚注册映射(键-类)。
  • HoloTableViewMGPlugin 来支持 MGSwipeTableCell,为 cell 添加滑动动作。
  • 差异重新加载数据。使用 HoloTableViewDiffPlugin 支持 DeepDiff
  • 支持现代 Objective-C 和更好的 Swift。

示例

要运行示例项目,请先复制仓库,然后从示例目录运行 pod install 命令。

与第三方库集成

  • HoloTableViewMGPlugin - 支持使用 MGSwipeTableCell 的插件,为 cell 添加滑动操作。MGSwipeTableCell 是一个易于使用的 UITableViewCell 子类,允许显示具有各种切换效果的滑动按钮。
  • HoloTableViewDiffPlugin - 支持使用 DeepDiff 的插件,diff 重新加载 UITableView 的某个部分。《DeepDiff》可以展示两个集合之间的差异以及更改步骤。

使用方法

中文介绍文章介绍

1. 创建一个简单的cell列表

UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
[self.view addSubview:tableView];

[tableView holo_makeRows:^(HoloTableViewRowMaker * _Nonnull make) {
   // make a cell
   make.row(ExampleTableViewCell.class).model(NSDictionary.new).height(44);
   
   // make a list
   for (NSObject *obj in NSArray.new) {
       make.row(ExampleTableViewCell.class).model(obj).didSelectHandler(^(id  _Nullable model) {
           NSLog(@"did select row : %@", model);
       });
   }
}];

[tableView reloadData];

// etc.

使用 holo_makeRows: 方法创建行列表。每个 row 是一个 cell。有关行的更多属性请参阅:见 HoloTableViewRowMaker.hHoloTableRowMaker.h

cell的要求

遵从 HoloTableViewCellProtocol 协议,HoloTableView 将自动识别 cell 是否实现了这些方法并进行调用,常用的两个方法是

@required

// set the model to cell
// the model is the object passed in by make.model()
- (void)holo_configureCellWithModel:(id)model;


@optional

// return cell height( Priority is higher than: 'heightHandler' and 'height' of maker)
// the model is the object passed in by make.model()
+ (CGFloat)holo_heightForCellWithModel:(id)model;

请参阅 HoloTableViewCellProtocol 的更多方法:HoloTableViewCellProtocol

您也可以通过配置 configSELheightSEL 等属性来调用您自己的方法。更多属性请参阅 HoloTableRowMaker.h.

请注意,存在 SEL 的属性,如 heightestimatedHeight 等,具有优先级

  1. 首先判断 cell 是否实现了 heightSEL 方法
  2. 其次,验证 heightHandler 块的实现
  3. 最后,确定是否已分配了属性 height

2. 创建一个包含头部、尾部和行的列表

UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
[self.view addSubview:tableView];
    
[tableView holo_makeSections:^(HoloTableViewSectionMaker * _Nonnull make) {
    make.section(TAG)
    .header(ExampleHeaderView.class)
    .headerModel(NSDictionary.new)
    .footer(ExampleFooterView.class)
    .footerModel(NSDictionary.new)
    .makeRows(^(HoloTableViewRowMaker * _Nonnull make) {
        // make a cell
        make.row(ExampleTableViewCell.class).model(NSDictionary.new).height(44);
        
        // make a list
        for (NSObject *obj in NSArray.new) {
            make.row(ExampleTableViewCell.class).model(obj).didSelectHandler(^(id  _Nullable model) {
                NSLog(@"did select row : %@", model);
            });
        }
    });
}];
    
[tableView reloadData];

使用 holo_makeSections: 方法创建一个 section 列表。关于 section 的更多属性,请参阅:HoloTableViewSectionMaker.hHoloTableSectionMaker.h

头部和尾部的要求

  1. 头部:遵守协议 HoloTableViewHeaderProtocol,实现这些方法,常用的两个方法是:
@required

// set the model to header
// the model is the object passed in by make.headerModel()
- (void)holo_configureHeaderWithModel:(id)model;

@optional
    
// return header height( Priority is higher than: 'headerHeightHandler' and 'headerHeight' of maker)
// the model is the object passed in by make.headerModel()
+ (CGFloat)holo_heightForHeaderWithModel:(id)model;
  1. 尾部:遵守协议 HoloTableViewFooterProtocol,实现这些方法,常用的两个方法是:
@required

// set the model to footer
// the model is the object passed in by make.footerModel()
- (void)holo_configureFooterWithModel:(id)model;

@optional

// return footer height( Priority is higher than: 'footerHeightHandler' and 'footerHeight' of maker)
// the model is the object passed in by make.footerModel()
+ (CGFloat)holo_heightForFooterWithModel:(id)model;

更多关于 HoloTableViewHeaderProtocolHoloTableViewFooterProtocol 的方法,请参阅:HoloTableViewHeaderProtocolHoloTableViewFooterProtocol

您还可以通过配置如 headerConfigSELfooterConfigSEL 等属性来调用您自己的方法。更多属性请在 HoloTableSectionMaker.h 中查找。

cell 一样,包含 SEL 的属性也有优先级。

3. section 的方法

// adding
[self.tableView holo_makeSections:^(HoloTableViewSectionMaker * _Nonnull make) {
    make.section(TAG);
}];

// inserting at index
[self.tableView holo_insertSectionsAtIndex:0 block:^(HoloTableViewSectionMaker * _Nonnull make) {
    make.section(TAG);
}];

// updating with tag value by maker
[self.tableView holo_updateSections:^(HoloTableViewSectionMaker * _Nonnull make) {
    make.section(TAG);
}];

// resetting with tag value by maker
[self.tableView holo_remakeSections:^(HoloTableViewSectionMaker * _Nonnull make) {
    make.section(TAG);
}];

// deleting
[self.tableView holo_removeAllSections];

// deleting with tag value
[self.tableView holo_removeSections:@[TAG]];


// reloadData
[self.tableView reloadData];

UITableView+HoloTableView.h 为操作 sections 提供了一系列方法,包括添加、插入、更新、重置、删除等。更多关于 section 的方法,请参阅:UITableView+HoloTableView.h (关于 section)

4. 行的方法

// adding
[self.tableView holo_makeRows:^(HoloTableViewRowMaker * _Nonnull make) {
    make.row(ExampleTableViewCell.class);
}];

// adding to section with tag value
[self.tableView holo_makeRowsInSection:TAG block:^(HoloTableViewRowMaker * _Nonnull make) {
    make.row(ExampleTableViewCell.class);
}];

// inserting at index
[self.tableView holo_insertRowsAtIndex:0 block:^(HoloTableViewRowMaker * _Nonnull make) {
    make.row(ExampleTableViewCell.class);
}];

// inserting at index to section with tag value
[self.tableView holo_insertRowsAtIndex:0 inSection:TAG block:^(HoloTableViewRowMaker * _Nonnull make) {
    make.row(ExampleTableViewCell.class);
}];

// updating
[self.tableView holo_updateRows:^(HoloTableViewUpdateRowMaker * _Nonnull make) {
    make.tag(TAG).height(120);
}];

// resetting
[self.tableView holo_remakeRows:^(HoloTableViewUpdateRowMaker * _Nonnull make) {
    make.tag(TAG).model(NSDictionary.new).height(120);
}];

// deleting
[self.tableView holo_removeAllRowsInSections:@[TAG]];

// deleting
[self.tableView holo_removeRows:@[TAG]];


// reloadData
[self.tableView reloadData];

UITableView+HoloTableView.h 为操作行提供了一系列方法,包括添加、插入、更新、重置、删除等。更多关于行的方法,请参阅:UITableView+HoloTableView.h (关于行)

5. 获取代理

您可以在任何时候获取 UITableView 的代理,例如

// first way
self.tableView.holo_proxy.dataSource = self;
self.tableView.holo_proxy.delegate = self;
self.tableView.holo_proxy.scrollDelegate = self;

// second way
[self.tableView holo_makeTableView:^(HoloTableViewMaker * _Nonnull make) {
    make.dataSource(self).delegate(self).scrollDelegate(self);
}];

一旦您设置了 dataSourcedelegatescrollDelegate 并实现了它们的一些方法,HoloTableView 将首先使用您的方法和返回值。对于具体逻辑,请参阅:[HoloTableViewProxy.m](https://github.com/HoloFoundation/HoloTableView/blob/master/HoloTableView/Classes/HoloProxy/HoloTableViewProxy.m)

6. 注册键-Class 映射

HoloTableView 预先支持表头、表尾和行的键值映射。例如

// regist key-Class map
[self.tableView holo_makeTableView:^(HoloTableViewMaker * _Nonnull make) {
    make
    .makeHeadersMap(^(HoloTableViewHeaderMapMaker * _Nonnull make) {
        make.header(@"header1").map(ExampleHeaderView1.class);
        make.header(@"header2").map(ExampleHeaderView2.class);
        // ...
    })
    .makeFootersMap(^(HoloTableViewFooterMapMaker * _Nonnull make) {
        make.footer(@"footer1").map(ExampleFooterView1.class);
        make.footer(@"footer2").map(ExampleFooterView2.class);
        // ...
    })
    .makeRowsMap(^(HoloTableViewRowMapMaker * _Nonnull make) {
        make.row(@"cell1").map(ExampleTableViewCell1.class);
        make.row(@"cell2").map(ExampleTableViewCell2.class);
        // ...
    });
}];


// use the key value
[self.tableView holo_makeSections:^(HoloTableViewSectionMaker * _Nonnull make) {
    // section 1
    make.section(TAG1)
    .headerS(@"header1")
    .footerS(@"footer1")
    .makeRows(^(HoloTableViewRowMaker * _Nonnull make) {
        make.rowS(@"cell1");
        make.rowS(@"cell2");
    });
    
    // section 2
    make.section(TAG2)
    .headerS(@"header2")
    .footerS(@"footer2")
    .makeRows(^(HoloTableViewRowMaker * _Nonnull make) {
        make.rowS(@"cell1");
        make.rowS(@"cell2");
    });
    
    // ...
}];

如果您事先进行了 key-class 映射注册,则使用 headerSfooterSrowS 从注册的映射中获取 Class

如果没有注册,直接使用 headerSfooterSrowS,将传入的字符串通过 NSClassFromString(NSString * _Nonnull aClassName) 方法转换为 Class

安装

HoloTableView 通过 CocoaPods 提供。安装它,只需将以下行添加到您的 Podfile 中

pod 'HoloTableView'

作者

gonghonglou, [email protected]

许可协议

HoloTableView 在 MIT 许可协议下可用。有关更多信息,请参阅 LICENSE 文件。