UICollectionViewCell с проблемой расстояния между ячейками динамического размера - PullRequest
0 голосов
/ 06 июня 2018

Я создаю пользовательский UICollectionViewCell, его размер автоматически устанавливается из его содержимого.

Динамический размер ячейки работает, но в нем есть дополнительное пространство.

Так что я хочуудалить дополнительное пространство между двумя ячейками

Я также установил UICollectionView DataSource и Delegate and Impalement Это метод, но все еще не работает

enter image description here

В один ряд не исправляйте свои только 2 ячейки.Он имеет 1, 2 или 3 ячейку, это зависит от Text

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;
}

1 Ответ

0 голосов
/ 06 июня 2018

Попробуйте добавить динамический размер ячейки с помощью следующего метода делегата

- (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);
}
...