具有自动调整大小的 UICollectionViewCell 在 iOS 9 中不起作用,但在 iOS 10 中起作用

Posted

技术标签:

【中文标题】具有自动调整大小的 UICollectionViewCell 在 iOS 9 中不起作用,但在 iOS 10 中起作用【英文标题】:UICollectionViewCell With automatic sizing not Working in iOS 9 But Working in iOS 10 【发布时间】:2018-06-04 07:30:20 【问题描述】:

我想制作 UICollectionViewCell 的动态大小。

它在 iOS 10 上运行,但应用在 iOS 9 中崩溃。

我尝试了很多解决方案,但没有一个有效。

iPhone 5s 屏幕截图(工作中)

我想要 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

【问题讨论】:

请添加错误日志并显示您在哪一行崩溃 *** 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array”*** 第一次抛出调用堆栈: (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 跨度>终止 返回单元格后崩溃 你能说明你在哪里初始化dataArr,你在哪里改变它? 我在 viewDidLoad 方法上看不到它 【参考方案1】:

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

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;

【讨论】:

以上是关于具有自动调整大小的 UICollectionViewCell 在 iOS 9 中不起作用,但在 iOS 10 中起作用的主要内容,如果未能解决你的问题,请参考以下文章

创建具有自动调整大小的文本区域

如何在 Chart.js 2.9.3 中绘制具有动态厚度的水平条(在浏览器窗口高度调整大小时自动调整大小)?

在具有自动布局的 UITableViewCell 中动态调整大小的 UIWebView - 违反约束

使用具有自动调整大小的默认整数数组在java中实现堆栈

具有自动调整大小的多个文本区域在 ionic3 中不起作用

UIImageView 在具有不同高度的自定义 UITableViewCell 内自动调整大小