【问题标题】:UICollectionViewCell With Dynamic Size Cell Spacing Issue具有动态大小单元格间距问题的 UICollectionViewCell
【发布时间】:2018-06-06 13:09:59
【问题描述】:

我正在创建自定义 UICollectionViewCell,它的大小会根据其内容自动设置。

单元格的动态大小有效,但它有额外的空间。

所以我想删除两个单元格之间的多余空间

我也设置了 UICollectionView DataSource 和 Delegate 和 Impalement 它的方法,但它仍然不工作

In One Row 不修复他们唯一的 2 个单元格。它有 1、2 或 3 个单元格,取决于文本

CustomeCollectionViewCell.m

- (CGSize)intrinsicContentSize
{
    CGSize size = self.lblText.intrinsicContentSize;
    size.width += 48;   // add padding Width that Given in Cell
    size.height += 32;  // add padding Height
    return size;
}

ViewController.m

@interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (strong, nonatomic) CustomeCollectionViewCell *tempCell;
@property (strong, nonatomic) NSArray *dataArr;
@end


- (void)viewDidLoad {
    [super viewDidLoad];

    self.dataArr = @[@"abcdef”,@"abcdef”,@"abcdef”,@"abcdef”,@"abcdef”];
    self.tempCell = (CustomeCollectionViewCell*) [[[UINib nibWithNibName:@“CustomeCollectionViewCell" bundle:nil] instantiateWithOwner:self options:nil] firstObject];

    [self.collectionView setContentInset:UIEdgeInsetsMake(8, 8, 8, 8)];
    [self.collectionView registerNib:[UINib nibWithNibName:@"CustomeCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CustomeCollectionViewCell"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CustomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CustomeCollectionViewCell" forIndexPath:indexPath];
    cell.lblText.text = [self.dataArr objectAtIndex:indexPath.item];
    cell.backgroundColor = [UIColor blackColor];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    self.tempCell.lblText.text = [self.dataArr objectAtIndex:indexPath.item];
    return self.tempCell.intrinsicContentSize;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 8;
}

【问题讨论】:

    标签: ios objective-c uicollectionview uicollectionviewcell


    【解决方案1】:

    尝试使用以下委托方法添加单元格的动态大小

    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    {
    
        return CGSizeMake((self.view.frame.size.width - 15)/2, (self.view.frame.size.width - 15)/2);
    }
    

    【讨论】:

    • 一行中不修复 2 个单元格或三个单元格,这取决于内容
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    相关资源
    最近更新 更多