美文网首页
UICollectionView -Base

UICollectionView -Base

作者: shier | 来源:发表于2021-01-13 17:15 被阅读0次

    UICollectionView简单使用用法

    - (UICollectionView *)collectionView {
        if (!_collectionView) {
            UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
            layout.scrollDirection = UICollectionViewScrollDirectionVertical;
            layout.minimumLineSpacing = 8;
            layout.minimumInteritemSpacing = 8;
            layout.sectionInset = UIEdgeInsetsMake(self.cellTopMargin, 15, 12, 24);//section set
            CGFloat itemCellWidth = (ScreenWidth-15-24-3*8)/4;
            layout.itemSize = CGSizeMake(itemCellWidth, 30);
            layout.headerReferenceSize = CGSizeMake(ScreenWidth, self.sectionHeaderHeight);
            
            _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
            _collectionView.dataSource = self;
            _collectionView.delegate = self;
            _collectionView.scrollsToTop = NO;
            _collectionView.showsVerticalScrollIndicator = NO;
            _collectionView.showsHorizontalScrollIndicator = NO;
            [_collectionView registerClass:[FHMatchFilterHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionHeaderIdentifier];
            
            [_collectionView registerClass:[FHMatchFilterCell class] forCellWithReuseIdentifier:kCollectionCellIdentifier];
            _collectionView.backgroundColor = [UIColor clearColor];
        }
        return _collectionView;
    }
    
    #pragma mark - UICollectionViewDelegate & UICollectionViewDataSource
    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
        return self.cityList.count;
    }
    
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
        NSDictionary *cityDict = [self.cityList objectAtIndex:section];
        NSArray *cityGroup = [cityDict valueForKey:[self.indexData objectAtIndex:section]];
        return cityGroup.count;
    }
    
    //下面注释的内容被上面的layout属性替换,就可以省略不写
    //- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
    //{
    //    return UIEdgeInsetsMake(self.cellTopMargin, 15, 12, 24);
    //}
    //- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
    //{
    //    return 8;
    //}
    //
    //- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
    //{
    //    return 8;
    //}
    //- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    //{
    //    float itemCellWidth = (ScreenWidth-15-24-3*8)/4;
    //    return CGSizeMake(itemCellWidth, 30);
    //
    //}
    
    //cell
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        FHMatchFilterCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionCellIdentifier forIndexPath:indexPath];
        NSDictionary *cityDict = [self.cityList objectAtIndex:indexPath.section];
        NSArray *cityGroup = [cityDict valueForKey:[self.indexData objectAtIndex:indexPath.section]];
        [cell updateCityName:[cityGroup objectAtIndex:indexPath.row]];
        return cell;
    }
    
    //section 头部高度
    //- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    //    return CGSizeMake(ScreenWidth, self.sectionHeaderHeight);
    //}
    
    //section 头部view
    - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
    {
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
            FHMatchFilterHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:kCollectionHeaderIdentifier forIndexPath:indexPath];
            NSString *title = self.indexData[indexPath.section];
            [header updateCityIndex:title];
            return header;
        }
        return nil;
    }
    
    

    相关文章

      网友评论

          本文标题:UICollectionView -Base

          本文链接:https://www.haomeiwen.com/subject/xeqpaktx.html