【问题标题】:UICollectionView delegate method cellForItemAtIndexPath:indexPath not called from sizeForItemAtIndexPathUICollectionView 委托方法 cellForItemAtIndexPath:indexPath 未从 sizeForItemAtIndexPath 调用
【发布时间】:2012-12-31 21:35:39
【问题描述】:

当我打电话时

[collectionView cellForItemAtIndexPath:indexPath:]

从内部

[collectionView:layout:sizeForItemAtIndexPath:]

那么委托方法不会被触发。知道为什么不?

你可以看到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 preferredSize];
}

我想做的是询问单元格的首选大小。

我可以的

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

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

为什么它的委托方法没有被调用?

【问题讨论】:

    标签: ios objective-c cocoa-touch ios6 uicollectionview


    【解决方案1】:

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

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

    导致循环循环。

    【讨论】:

    • 我在 iOS6 上有那个循环!感谢您的提示
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    相关资源
    最近更新 更多