集合视图,类似于UITableView或UICollectionView,增强了布局能力,支持多种布局方式,且可以在同一个视图上使用多种布局方式,可以替代UITableView或UICollectionView。
可以同时使用多种布局方式
类似于UITableView或UICollectionView
支持多种布局方式,包括Row、Grid、WaterFlow、Flow等
自定义布局
元素复用、预加载.
支持下拉刷新和上拉刷新
适用于行布局的UITableView。
适用于网格/流布局的UICollectionView和UICollectionViewFlowLayout。
适用于自定义布局的UICollectionView。
使用 CocoaPods,将以下行添加到您的 Podfile
中。
pod 'FMCollectionView'
然后运行 pod install
,您就完成所有操作了!
或者将 Sources
文件夹下的 *.h *.m 文件复制到您的项目中。
例如。
- (void)viewDidLoad {
[super viewDidLoad];
self.collectionView = [[FMCollectionView alloc] initWithFrame:self.view.bounds];
self.collectionView.sectionsSpacing = 20;
self.collectionView.itemsSpacing = 4;
self.collectionView.delegatesAndDataSource = self;
[self.view addSubview:self.collectionView];
}
FMCollectionViewDelegatesAndDataSource
,在 FMCollectionViewDataSource
中只需要两个方法,其他是可选的。- (NSInteger)numberOfSectionsInCollectionView:(FMCollectionView *)collectionView {
return HomeSectionsTotalCount;
}
- (NSInteger)collectionView:(FMCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (section == HomeSectionsBanners) {
return self.presenter.banners.count > 0 ? 1 : 0;
} else if (section == HomeSectionsCategories) {
return self.presenter.categories.count;
} else if (section == HomeSectionsProducts){
return [self.presenter itemsCount];
} else if (section == HomeSectionsProductsLoadFailed){
return self.presenter.isFetchProductsFailed ? 1 : 0;
}
return 0;
}
- (UIView *)collectionView:(FMCollectionView *)collectionView itemAtIndexPath:(NSIndexPath *)indexPath {
[self.collectionView beginBottomRefleshingIfNeed:indexPath];
if (indexPath.section == HomeSectionsBanners) {
return self.bannersComponent.componentView;
} else if (indexPath.section == HomeSectionsCategories) {
FMBannerModel *model = self.presenter.categories[indexPath.row];
HomeCategoryCell *cell = [collectionView dequeueReusableItemWithId:@"categories_cell" atIndexPath:indexPath itemClass:HomeCategoryCell.class];
[cell setImgUrl:model.onlyPic.picUrl];
return cell;
} else if (indexPath.section == HomeSectionsProducts) {
ProductListModel *data = [self.presenter itemAtIndex:indexPath.row];
HomeProductCell *cell = [collectionView dequeueReusableItemWithId:@"products_cell" atIndexPath:indexPath itemClass:HomeProductCell.class];
[cell.imgView setImgUrl:data.picUrl];
cell.titleLabel.text = data.productShortTitle;
cell.priceLabel.text = [FMBizUtils stringFromPriceInCent:data.minPriceCent];
return cell;
} else if (indexPath.section == HomeSectionsProductsLoadFailed) {
WeakSelf()
FMNoResultBGViewItem *item = [[FMNoResultBGViewItem alloc] init];
item.style = kNoResultBGViewStyleImgTitle;
item.clicked = ^{
[weakSelf.presenter loadAllKindsData];
};
return [[FMNoResultBGView alloc] initWithFrame:collectionView.bounds andItem:item];
} else {
return nil;
}
}