iOS 常用代码之 UICollectionView

本文介绍如何使用SwiftUI实现热门搜索与历史搜索功能,包括界面布局、数据展示及头部视图配置等关键技术点。
记一下 不用每次都从0开始写,人生苦短 ,省点时间给自己



之前必须完成相关注册:
1. cell 2. 头部和尾部 [self.hotAndHistoryCollectionV registerNib:[UINib nibWithNibName:@"HotAndHistoryCell" bundle:nil] forCellWithReuseIdentifier:@"hotSearchCellID"]; [self.hotAndHistoryCollectionV registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headViewID"]; #pragma mark --- hot search collectionViewDelegate --- - (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath { HotAndHistoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"hotSearchCellID" forIndexPath:indexPath]; //cell.backgroundColor = [UIColor redColor]; //cell.contentView.backgroundColor = [UIColor blackColor]; return cell; } - (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 6; } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 2; } -(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ if ([kind isEqual:UICollectionElementKindSectionHeader]) { //UIView *headView = [[UIView alloc]init]; UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headViewID" forIndexPath:indexPath]; if (indexPath.section == 0) { UILabel *label = [[UILabel alloc] init]; label.frame = CGRectMake(12,10,110,20); label.text = @"热门搜索"; label.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:14]; label.textColor = [UIColor colorWithRed:21/255.0 green:23/255.0 blue:27/255.0 alpha:1]; [headView addSubview:label]; } else { UIView *view = [[UIView alloc] init]; view.frame = CGRectMake(0,0,self.view.frame.size.width,8); view.backgroundColor = [UIColor colorWithRed:249/255.0 green:249/255.0 blue:251/255.0 alpha:1]; [headView addSubview:view]; UILabel *label = [[UILabel alloc] init]; label.frame = CGRectMake(12,18,110,20); label.text = @"历史搜索"; label.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:14]; label.textColor = [UIColor colorWithRed:21/255.0 green:23/255.0 blue:27/255.0 alpha:1]; [headView addSubview:label]; UIButton *trashBtn = [UIButton new]; trashBtn.frame = CGRectMake(headView.frame.size.width - 12 - 20, 18, 20, 20); [trashBtn setImage:[UIImage imageNamed:@"search_iconbtn_delete_default"] forState:UIControlStateNormal]; [headView addSubview:trashBtn]; } return headView; } return nil; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{ if (section == 0) { return CGSizeMake(self.view.frame.size.width, 30); } else{ return CGSizeMake(self.view.frame.size.width, 38); } } //定义每个UICollectionView 的大小 - ( CGSize )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:( NSIndexPath *)indexPath { return CGSizeMake((self.view.frame.size.width - 12 * 6)/3,34); } //定义每个UICollectionView 的边距 - ( UIEdgeInsets )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:( NSInteger )section { return UIEdgeInsetsMake ( 9 , 12 , 9 , 12 ); } ////设置水平间距 (同一行的cell的左右间距) //-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { // return 9; //} ////垂直间距 (同一列cell上下间距) //- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { // return 9; //}

 

转载于:https://www.cnblogs.com/liuw-flexi/p/9151569.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值