![](/img/trans.png)
[英]sizeForItemAtIndexPath and cellForItemAtIndexPath is not called
[英]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.