Ios 如何更改collectionview中的数据和布局?

Ios 如何更改collectionview中的数据和布局?,ios,objective-c,uicollectionview,uicollectionviewlayout,Ios,Objective C,Uicollectionview,Uicollectionviewlayout,我有一个collectionview控制器。我想重复使用它。我有两个自定义的collectionviewlayouts,每个都有自己的数据源 我应该采取什么步骤和顺序来更改布局和数据源 我的CollectionView控制器如下所示: - (id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout { self = [super initWithCollectionViewLayout:layout]; if

我有一个collectionview控制器。我想重复使用它。我有两个自定义的collectionviewlayouts,每个都有自己的数据源

我应该采取什么步骤和顺序来更改布局和数据源

我的CollectionView控制器如下所示:

- (id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout {
    self = [super initWithCollectionViewLayout:layout];
    if (self) {
        self.dataSource = [[WeekDataSource alloc] init];
    }
    return self;
}
- (void)loadView {
    [super loadView];
    [self setupCollectionView];
}
- (void)setupCollectionView {
    self.collectionView = [[CalendarView alloc] initWithFrame:self.view.frame collectionViewLayout:self.collectionViewLayout];
    self.collectionView.dataSource = self.dataSource;
    self.collectionView.delegate = self.dataSource;
    self.collectionView.backgroundColor = [UIColor whiteColor];
}
控制器使用从
UICollectionViewLayout
派生的自定义布局显示集合视图

我遇到的问题是改变布局和它所依赖的数据源

我已经尝试了以下方法,但出于某种原因,collectionView坚持使用旧的collectionViewLayout

self.dataSource = [[DayDataSource alloc] initWithScheduleNumber:0];
self.collectionView.dataSource = self.dataSource;
self.collectionView.collectionViewLayout = layout;

编辑:我发现UICollectionViewController的
self.collectionViewLayout
是只读属性。这是否意味着控制器不打算以这种方式重复使用?(通过在运行时关闭布局和数据源来重用)?我认为它指向用于初始化控制器的布局,因此视图尝试使用旧布局的问题。

我找到了这个问题

下面是允许重用控件的代码

- (void)switchToViewMode:(NSInteger)viewMode {
    UICollectionViewLayout *layout;
    CalendarView *view;
    CGRect oldFrame;
        switch (viewMode) {
            case 0:
            case 1:
                layout = [[DayCollectionViewLayout alloc] initWithCoder:nil];
                oldFrame = self.collectionView.frame;
                view = [[CalendarView alloc] initWithFrame:oldFrame collectionViewLayout:layout];
                self.dataSource = [[DayDataSource alloc] initWithScheduleNumber:0]; // TODO set correct date
                view.dataSource = self.dataSource;
                view.backgroundColor = [UIColor whiteColor];
                self.collectionView = view;
                break;

            case 2:
                layout = [[WeekCollectionViewLayout alloc] initWithCoder:nil];
                oldFrame = self.collectionView.frame;
                view = [[CalendarView alloc] initWithFrame:oldFrame collectionViewLayout:layout];
                self.dataSource = [[WeekDataSource alloc] init];
                view.dataSource = self.dataSource;
                view.backgroundColor = [UIColor whiteColor];
                self.collectionView = view;
                break;

            default:
                    @throw [NSException exceptionWithName:NSInternalInconsistencyException
                                                   reason:@"Unknown view mode selected." userInfo:nil];
                break;
        }
}