iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – UICollectionView委托方法cellForItemAtIndexPath:未从sizeForItemAtIndexPath调用indexPath大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_675_2@
我打电话的时候

[collectionView cellForItemATindexPath:indexPath:]

从内部

[collectionView:layout:sizeForItemATindexPath:]

然后不会触发委托方法.知道为什么不呢?

你可以看到它here.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemATindexPath:(NSIndexPath *)indexPath {

    Customcatell *cell = [self.collectionView dequeueReusableCellWithReusEIDentifier:@"Customcatell" forIndexPath:indexPath];

    [cell configureWithData:self.data[indexPath.row]];

    return cell;
}


- (CGSizE)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemATindexPath:(NSIndexPath *)indexPath {

    Customcatell * cell = (Customcatell *)[collectionView cellForItemATindexPath:indexPath];

    return [cell preferredSize];
}

我想要做的是询问电池的首选尺寸.

我可以

Customcatell * cell = (Customcatell *)[self collectionView:collectionView cellForItemATindexPath:indexPath];

但随后会触发一个永无止境的循环周期

为什么它的委托方法不应该被调用呢?

@H_675_2@

解决方法

我最终得到了一个Class方法而不是实例方法

- (CGSizE)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemATindexPath:(NSIndexPath *)indexPath {

    return [Customcatell preferredSizeWithData:self.data[indexPath.row]; 

}

我为单元格创建了一个Class方法…对于这个方法,我提供了指定indexPath上的实际实例将保存并计算首选大小的数据

我觉得

Customcatell * cell = (Customcatell *)[collectionView cellForItemATindexPath:indexPath];

触发内部

- (CGSizE)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemATindexPath:(NSIndexPath *)indexPath

而我们所看到的是Apple的一些机制来阻止循环循环…因为直接调用

Customcatell * cell = (Customcatell *)[self collectionView:collectionView cellForItemATindexPath:indexPath];

导致循环周期.

@H_675_2@ @H_675_2@
@H_675_2@
@H_675_2@

大佬总结

以上是大佬教程为你收集整理的ios – UICollectionView委托方法cellForItemAtIndexPath:未从sizeForItemAtIndexPath调用indexPath全部内容,希望文章能够帮你解决ios – UICollectionView委托方法cellForItemAtIndexPath:未从sizeForItemAtIndexPath调用indexPath所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。