Моя проблема решена тем, что некоторые изменения в коде и реализация некоторого метода, который доступен в данном коде.
1) реализует intrinsicContentSize метод в CustomeCollectionViewCell
2) установить значение tempCell от полученного размера в sizeForItemAtIndexPath метод
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 = @[@"a",@"bc",@"def",@"ghijkl",@"mkopqrst",@"uvwxyz"];
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 8;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 8;
}