UICollectionViewCell自动调整大小不适用于iOS 9但在iOS 10中工作

时间:2018-06-04 07:30:21

标签: ios objective-c uicollectionview uicollectionviewcell

我想制作UICollectionViewCell的动态大小。

它正在 iOS 10 上工作但是应用程序在 iOS 9 中崩溃。

我尝试了很多解决方案,但都没有。

iPhone 5s的屏幕截图(工作) enter image description here

我想要iOS 9上的输出

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.collectionView setContentInset:UIEdgeInsetsMake(8, 8, 8, 8)];
    [self.collectionView registerNib:[UINib nibWithNibName:@"CustomeCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CustomeCollectionViewCell"];
    UICollectionViewFlowLayout *flowLayout =  (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
    flowLayout.estimatedItemSize = CGSizeMake(1, 1);
    flowLayout.minimumLineSpacing = 8.0f;
    flowLayout.minimumInteritemSpacing = 8.0f;
}

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

错误日志

  

*由于未捕获的异常终止应用' NSRangeException',原因:' * - [__ NSArrayM objectAtIndex:]:索引0超出空数组的范围' ***第一掷调用堆栈:(0x2592791b 0x250c2e17 0x2583972b 0x2a6faba7 0x2a6b8adf 0x2a698753 0x2a698d87 0x29f08d57 0x29f06ed3 0x29f01fe9 0x29e9ea73 0x27f36bcd 0x27f32375 0x27f32209 0x27f316d1 0x27f313a5 0x29e95b79 0x258e96c9 0x258e79cd 0x258e7dff 0x25837229 0x25837015 0x26e27ac9 0x29f0b189 0x66f2d 0x254df873)的libc ++ abi.dylib:与类型NSException的未捕获的异常终止

1 个答案:

答案 0 :(得分:0)

我的问题通过代码中的一些更改来解决,并实现了给定代码中可用的某些方法。

1)在CustomeCollectionViewCell中实现 intrinsicContentSize 方法

2)在 sizeForItemAtIndexPath 方法

中设置 tempCell 的值

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