当我打电话时
[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];
但随后会触发一个永无休止的循环
为什么它的委托方法没有按应有的方式被调用?
我最终得到了类方法而不是实例方法:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return [CustomCell preferredSizeWithData:self.data[indexPath.row];
}
我为单元格创建了一个类方法...对于这个方法,我提供了指定的实际实例的数据indexPath
将保存并计算首选尺寸
我觉得
CustomCell * cell = (CustomCell *)[collectionView cellForItemAtIndexPath:indexPath];
内部触发
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
我们看到的是苹果公司的一些机制来防止循环......因为直接调用
CustomCell * cell = (CustomCell *)[self collectionView:collectionView cellForItemAtIndexPath:indexPath];
导致循环。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)