手风琴样式表格视图,基于块的服务委托。
CocoaPods 马上就会,但现在您可以先将 PDTiledView.h/.m
添加到您的项目中。
与 UITableView
非常相似,但是使用 sections
和 tiles
而不是 sections
和 rows
。它还使用块来代替协议进行委托。
PDTiledView *tiledView = ...;
tiledView.numberOfSectionsBlock = ^NSInteger { return 4; };
tiledViewdView.numberOfTilesInSectionBlock = ^NSInteger (NSInteger section) { return 20; };
所有 sections
和 tiles
都是 UIControl
的子类,例如 UIButton
或您自己制作的自定义控件。(这可能很快会切换到 UIView,尚未确定)。
tiledView.controlForSectionBlock = ^UIControl *(NSInteger section) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor whiteColor];
return button;
};
tiledView.controlForTileAtIndexPathBlock = ^UIControl *(PDIndexPath indexPath) {
UIButton *button = [UIButton buttonWithType:UIControlIButtonTypeCustom];
return button;
};
基于块的服务委托的结果,您应该在视图首次显示时显式调用 reloadData
。
[tiledView reloadData];
您也可以通过调用 selectSection:animated:
来程序化选择一个章节。
[tiledView selectSection:0 animated:NO];
还有一些可选的块可以进一步自定义您喜欢的样式。它们与 UITableViewDelegate/DataSource
的对应物相匹配
heightForSectionControlBlock
heightForTilesInSectionBlock
didSelectSectionBlock
didSelectTileAtIndexPathBlock
willDisplaySectionBlock
内部实现不使用 UITableView
,因此虽然一些内容被缓存,但 tile 不是像在 UITableView 中的行那样动态加载和缓存的。如果不显示数千个 tile 或者 tile 是极其渲染密集的,这不会有什么大问题。欢迎提交拉取请求以帮助实现缓存,或者可能使用内部的 UITableView
。
这也意味着 controlForSectionBlock
和 controlForTileAtIndexPathBlock
不会多次调用,通常只会调用一次。