iOS 常用代码之 UICollectionView

记一下 不用每次都从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;
//}
 

 

posted @ 2018-06-07 16:49  liuw_flexi  阅读(230)  评论(0编辑  收藏  举报