制作一个用于多个组件的集合控制器。为构建快速灵活的列表提供 UICollectionView/UITableView
框架。类似于 IGList
。
为了便于使用,API 与 UICollectionView
和 UITableView
类似。
@interface YourComponent : DDCollectionViewSectionComponent
@end
@implementation
- (instancetype)init
{
self = [super init];
if (self) {
self.size = CGSizeMake(DDComponentAutomaticDimension, 44);
// config here.
// Remember self.collectionView is nil until it is added to root component.
}
return self;
}
- (void)prepareCollectionView {
[super prepareCollectionView];
// register your cell here.
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 1;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return ... // Return your cell
}
@end
其他 API 也是一样的。
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
// select item
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
// will display
}
po self.rootComponent
<DDCollectionViewRootComponent: 0x6080000bb900>
[SubComponents]
<DDComponentPodDemo.StatusComponent: 0x60800009b440>
-[loading] <DDComponentPodDemo.StateComponent: 0x6080001651c0>
*[normal] <DDCollectionViewSectionGroupComponent: 0x60800009acc0>
[SubComponents]
<DDComponentPodDemo.TaobaoBannerComponent: 0x6080001235c0>
<DDComponentPodDemo.TaobaoEntriesComponent: 0x6080001524e0>
<DDComponentPodDemo.TaobaoGoodsComponent: 0x608000151eb0>
<DDComponentPodDemo.TaobaoGoodsComponent: 0x608000151510>
[Header] <DDComponentPodDemo.HeaderComponent: 0x608000151250>
<DDComponentPodDemo.TaobaoGoodsComponent: 0x608000152220>
<DDComponentPodDemo.TaobaoGoodsComponent: 0x608000152380>
<DDComponentPodDemo.TaobaoGoodsComponent: 0x6080001511a0>
<DDComponentPodDemo.TaobaoGoodsComponent: 0x608000151b40>
[Header] <DDComponentPodDemo.HeaderComponent: 0x608000151ca0>
<DDComponentPodDemo.TaobaoGoodsComponent: 0x608000152170>
<DDComponentPodDemo.TaobaoGoodsComponent: 0x6080001515c0>
<DDComponentPodDemo.TaobaoGoodsComponent: 0x608000151a90>
[Header] <DDComponentPodDemo.HeaderComponent: 0x608000151f60>
<DDComponentPodDemo.TaobaoGoodsComponent: 0x608000151930>
<DDComponentPodDemo.TaobaoGoodsComponent: 0x608000151670>
<DDComponentPodDemo.TaobaoGoodsComponent: 0x608000152640>
-[error] <DDComponentPodDemo.StateComponent: 0x608000165340>
-[noData] <DDComponentPodDemo.StateComponent: 0x608000165400>
我们只需要做的是这个红色部分。
首先使用 size
和 inset
,然后覆盖 delegate
API。
由于使用系统 API,indexPath
是 UICollectionView
的位置,而不是组件 dataSource 的索引。
0.5.2 优化 numberOf
。添加 dataSource 缓存和 collectionView 缓存。
0.5.1 只支持 UICollectionView
。
0.5.0 新版 DDComponent 采用了 Objective-C。