记一下 不用每次都从0开始写,人生苦短 ,省点时间给自己

之前必须完成相关注册:
. cell
. 头部和尾部 [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 ;
} - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return ;
} -(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 == ) {
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(,,,);
label.text = @"热门搜索";
label.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:];
label.textColor = [UIColor colorWithRed:/255.0 green:/255.0 blue:/255.0 alpha:];
[headView addSubview:label];
}
else {
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(,,self.view.frame.size.width,);
view.backgroundColor = [UIColor colorWithRed:/255.0 green:/255.0 blue:/255.0 alpha:];
[headView addSubview:view]; UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(,,,);
label.text = @"历史搜索";
label.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:];
label.textColor = [UIColor colorWithRed:/255.0 green:/255.0 blue:/255.0 alpha:];
[headView addSubview:label]; UIButton *trashBtn = [UIButton new];
trashBtn.frame = CGRectMake(headView.frame.size.width - - , , , );
[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 == ) {
return CGSizeMake(self.view.frame.size.width, );
}
else{
return CGSizeMake(self.view.frame.size.width, );
} } //定义每个UICollectionView 的大小
- ( CGSize )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:( NSIndexPath *)indexPath {
return CGSizeMake((self.view.frame.size.width - * )/,);
}
//定义每个UICollectionView 的边距
- ( UIEdgeInsets )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:( NSInteger )section {
return UIEdgeInsetsMake ( , , , );
}
////设置水平间距 (同一行的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;
//}
05-29 01:15