美文网首页
collection行高计算

collection行高计算

作者: marlonxlj | 来源:发表于2019-06-03 14:14 被阅读0次
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ConvePayCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ConvePayCollectionCellID forIndexPath:indexPath];
    if (self.dataSource.count > 0) {
        NSDictionary *dict = self.dataSource[indexPath.item];
        cell.infoDict = dict;
    }
    return cell;
}

//item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    return CGSizeMake(((SCREEN_WIDTH-30 * 3-40)/4), 90);
}

//调节item边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = self.dataSource[indexPath.item];
    
    KKLog(@"选择了%@",dict);
}

- (void)setDataSource:(NSArray *)dataSource
{
    _dataSource = dataSource;
    
    [self.collectionView reloadData];
    
    NSInteger count = self.dataSource.count;
    
    // 设置collectionView 计算collectionView高度 = rows * itemWH
    //万能公式: 行数= (个数-1)除以列数 + 1;
    // Rows = (count - 1) / cols + 1  3 cols4
    
    NSInteger rows = (count - 1) / 3 + 1;
    if (count > 4) {
        self.collectionHeight.constant = rows * 90 + rows * 10;
    }
    else{
        self.collectionHeight.constant = 90;
    }
}

相关文章

网友评论

      本文标题:collection行高计算

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