UICollectionView委托方法cellForItemAtIndexPath:indexPath未从sizeForItemAtIndexPath调用 [英] UICollectionView delegate method cellForItemAtIndexPath:indexPath not called from sizeForItemAtIndexPath

查看:5597
本文介绍了UICollectionView委托方法cellForItemAtIndexPath:indexPath未从sizeForItemAtIndexPath调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我调用

  [collectionView cellForItemAtIndexPath:indexPath:] 
pre>

从内部

  [collectionView:layout:sizeForItemAtIndexPath:] 

那么委托方法不会被触发。任何想法为什么不?



您可以看到 here

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

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

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

return cell;
}


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

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

return [cell prefferedSize];
}

我想要做的是要求单元格的优先大小。 / p>

我可以做

  CustomCell * cell = self collectionView:collectionView cellForItemAtIndexPath:indexPath]; 

但触发了一个永无止境的循环。



为什么它的委托方法不会被调用,因为它应该是?

解决方案

实例方法

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

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

}

我为单元格创建了Class方法...这个方法我提供在指定的indexPath的实例实例将持有并计算优先的大小的数据



i

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

触发internaly

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

我们看到的是一些来自苹果的机制,以防止循环...因为直接调用

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

会导致循环周期


When I call

[collectionView cellForItemAtIndexPath:indexPath:]

from inside

[collectionView:layout:sizeForItemAtIndexPath:]

then the delegate method is not triggered. Any idea why not?

You can see it here.

- (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 prefferedSize];
}

What i want to do is to ask the cell for its preffered size.

I could do

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

but then a never ending loop cycle is triggered

why is its delegate method not called as it should be?

解决方案

i ended up with a Class method instead of an instance method

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

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

}

i made a Class method for the cell... to this method i provide the data that an acctual instance at the specified indexPath would hold and calculate the preffered size

i thing that

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

triggers internaly

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

and what we see is some mechanism from Apple to prevent a loop cycle... because calling directly

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

results in a loop cycle

这篇关于UICollectionView委托方法cellForItemAtIndexPath:indexPath未从sizeForItemAtIndexPath调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆