Я создаю пользовательский UICollectionViewCell, его размер автоматически устанавливается из его содержимого.
Динамический размер ячейки работает, но в нем есть дополнительное пространство.
Так что я хочуудалить дополнительное пространство между двумя ячейками
Я также установил UICollectionView DataSource и Delegate and Impalement Это метод, но все еще не работает
![enter image description here](https://i.stack.imgur.com/CtNNW.png)
В один ряд не исправляйте свои только 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;
}