过渡到其他collectionviewController时collectionviewCell不会改变

Posted

技术标签:

【中文标题】过渡到其他collectionviewController时collectionviewCell不会改变【英文标题】:collectionviewCell not change when transition to other collectionviewController 【发布时间】:2013-10-30 08:37:41 【问题描述】:

我创建了 2 个 UICollectionView 类,每个类使用 2 个不同的 UICollectionViewCell

@interface PhotosCollectionViewController : UICollectionViewController
@interface FullScreenCollectionViewController : UICollectionViewController

@interface PhotoCell : UICollectionViewCell
@interface FullPhotoCell : UICollectionViewCell<UIScrollViewDelegate>

PhotosCollectionViewController.m中,我注册了PhotoCell类并在didSelect时选择下一个视图控制器是FullScreenCollectionViewController

//Register Cell
-(id)initWithCollectionViewLayout:(UICollectionViewFlowLayout *)layout
    if (self = [super initWithCollectionViewLayout:layout])
            
        [self.collectionView registerClass:[PhotoCell class] forCellWithReuseIdentifier:CELL_ID];
    
    return self;

//dequence resuse code
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

    PhotoCell* cell = (PhotoCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CELL_ID forIndexPath:indexPath];
    NSLog(@"reuse CELL");
    FICDPhoto *photo = [_photos objectAtIndex:indexPath.row];
    cell.imageView.image = [photo sourceImage];
    return cell;

//Next ViewController transition code
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

    UIViewController *vc = [self nextViewControllerAtPoint:CGPointZero];
    [self.navigationController pushViewController:vc animated:YES];



-(UICollectionViewController*)nextViewControllerAtPoint:(CGPoint)p

    FullScreenCollectionViewController* nextCollectionViewController = [[FullScreenCollectionViewController alloc] initWithCollectionViewLayout:[[FullScreenFlowLayout alloc] init]];

    nextCollectionViewController.useLayoutToLayoutNavigationTransitions = YES;

    nextCollectionViewController.title = @"FullScreen";
    return nextCollectionViewController;


FullScreenCollectionViewController中,我注册了FullPhotoCell类

[self.collectionView registerClass:[FullPhotoCell class] forCellWithReuseIdentifier:CELL_ID_FULL];

但序列码从不调用

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

    FullPhotoCell* cell = (FullPhotoCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CELL_ID_FULL forIndexPath:indexPath];
    NSLog(@"Reuse FULL Cell");
    FICDPhoto *photo = [_photos objectAtIndex:indexPath.row];
    cell.imageView.image = [photo sourceImage];
    return cell;

应用新布局,FullScreenCollectionViewController 的 viewDidload 也被调用,但日志消息“reuse CELL”告诉 fullScreen CollectionView 仍然使用旧的 PhotoCell 类。

我还是不明白有什么问题。请帮帮我!

【问题讨论】:

【参考方案1】:

答案“有点”晚了,但这可能对其他人有用。当您使用useLayoutToLayoutNavigationTransitions 时,来自视图控制器的集合视图启动转换实际上被重用。此集合视图具有不同的数据源和委托,这就是为什么不调用 FullScreenCollectionViewControllercellForRowAtIndexPath 的原因。看看这个类似的问题: UICollectionView UseLayoutToLayoutNavigationTransitions with different datasources

【讨论】:

以上是关于过渡到其他collectionviewController时collectionviewCell不会改变的主要内容,如果未能解决你的问题,请参考以下文章

丑陋的片段过渡到带有覆盖的surfaceview

过渡到 iOS 5 时要注意啥

2D过渡模块的其他属性

Vue学习笔记进阶篇——列表过渡及其他

Vue过渡状态

vue 过渡状态