#pragma mark - 设置 CollectionView
/** 设置 CollectionView */
-(void)setUpCollectionView{
self.backgroundColor = [UIColor colorFromHexRGB:@"EEEEEE"];
/** 左右间距 */
CGFloat lR = (self.width - floor(self.width / 3) * 3) / 2;
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, self.width - 2 * lR , self.frame.size.height - 64 - 49) collectionViewLayout:layout];
[self addSubview:self.collectionView];
/** 设置代理和数据源 */
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
/** 注册 cell */
[self.collectionView registerClass:[DYLiveCategoryCell class] forCellWithReuseIdentifier:liveID];
[self.collectionView registerClass:[DYRecentLiveCategoryCell class] forCellWithReuseIdentifier:recentID];
[self.collectionView registerClass:[DYLiveCategoryHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:DYLiveCategoryHeaderViewID];
self.collectionView.backgroundColor = [UIColor colorFromHexRGB:@"EEEEEE"];// QGColorWithHexString(@"efefef");
}
#pragma mark - 代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
if (section == 0) {
return 1;
}
return self.liveCategorys.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
DYRecentLiveCategoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:recentID forIndexPath:indexPath];
cell.recentCategoryDelegate = self;
cell.recentLiveCategoryModels = [DYLiveCategoryTool recentCategorys];
return cell;
}
DYLiveCategoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:liveID forIndexPath:indexPath];
SubColumnModel *liveCategoryModel = self.liveCategorys[indexPath.row];
cell.liveCategoryModel = liveCategoryModel;
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
return CGSizeMake(self.frame.size.width, 105);
}
return CGSizeMake(floor(self.collectionView.frame.size.width / 3), floor(self.collectionView.frame.size.width / 3));
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 0;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
/** 点击 cell 后取消选中 */
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
/** 取出选中的数据 */
SubColumnModel *liveCategoryModel = self.liveCategorys[indexPath.row];
/** 存储到最近使用 */
[DYLiveCategoryTool addRecentCategory:liveCategoryModel];
/** 取出第一个分区的 cell */
DYRecentLiveCategoryCell *cell = (DYRecentLiveCategoryCell * )[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
/** 重新赋值 */
cell.recentLiveCategoryModels = [DYLiveCategoryTool recentCategorys];
/** 返回到顶部的位置 */
[cell.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
[self pushWitModel:liveCategoryModel];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
return CGSizeMake(self.frame.size.width, 45);
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
if ([kind isEqual:UICollectionElementKindSectionHeader]) {
DYLiveCategoryHeaderView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:DYLiveCategoryHeaderViewID forIndexPath:indexPath];
headView.backgroundColor = [UIColor whiteColor];
headView.model = self.liveHeaders[indexPath.section];
return headView;
}
return nil;
}
-(void)DYRecentLiveCategoryCellDidSelectItemAtIndexPath:(NSIndexPath *)indexPath collectionView:(UICollectionView *)collectionView{
/** 点击 cell 后取消选中 */
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
/** 取出选中的数据 */
SubColumnModel *liveCategoryModel =[DYLiveCategoryTool recentCategorys][indexPath.row];
/** 存储到最近使用 */
[DYLiveCategoryTool addRecentCategory:liveCategoryModel];
/** 取出第一个分区的 cell */
DYRecentLiveCategoryCell *cell = (DYRecentLiveCategoryCell * )[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
/** 重新赋值 */
cell.recentLiveCategoryModels = [DYLiveCategoryTool recentCategorys];
/** 返回到顶部的位置 */
[cell.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
[self pushWitModel:liveCategoryModel];
}
-(void)pushWitModel:(SubColumnModel* )model{
DYClassifyLiveViewController *classifyVC = [[DYClassifyLiveViewController alloc] init];
classifyVC.catId = model.tag_id;
classifyVC.tagId = @"-1";
classifyVC.catName = model.tag_name;
[[ControllerTool getCurrentVC] pushViewController:classifyVC animated:YES];
}
/** 设置数据源 */
-(void)setCategorys:(NSMutableArray *)categorys{
_liveCategorys = categorys;
[self.collectionView reloadData];
}
UICollectionView 设置头标题和头标题的高度
最新推荐文章于 2024-06-26 16:18:05 发布