繁体   English   中英

UICollectionView委托方法cellForItemAtIndexPath:未从sizeForItemAtIndexPath调用indexPath

[英]UICollectionView delegate method cellForItemAtIndexPath:indexPath not called from sizeForItemAtIndexPath

我打电话的时候

[collectionView cellForItemAtIndexPath:indexPath:]

从内部

[collectionView:layout:sizeForItemAtIndexPath:]

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

你可以在这里看到它。

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

    CustomCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"CustomCell" forIndexPath:indexPath];

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

    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    CustomCell * cell = (CustomCell *)[collectionView cellForItemAtIndexPath:indexPath];

    return [cell preferredSize];
}

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

我可以

CustomCell * cell = (CustomCell *)[self collectionView:collectionView cellForItemAtIndexPath:indexPath];

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

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

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

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

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

}

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

我觉得

CustomCell * cell = (CustomCell *)[collectionView cellForItemAtIndexPath:indexPath];

触发内部

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 

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

CustomCell * cell = (CustomCell *)[self collectionView:collectionView cellForItemAtIndexPath:indexPath];

导致循环周期。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM