Pergunta

I have a collectionview controller. I would like to reuse it. I have a two custom collectionviewlayouts, each has its own data source.

What steps and in what order, do I take to change the layouts and datasource?

My CollectionView controller is as follows:

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

The controller displays the collection view just fine, using my custom layout which derives from UICollectionViewLayout.

What I am having troubles with, is changing out the layout, and the datasource which it depends on.

I have tried, the following, but for some reason the collectionView insists on using the old collectionViewLayout.

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

edit: I have discovered that self.collectionViewLayout of the UICollectionViewController is a read-only attribute. Does that mean the controller is not intended to be reused in this way? (reused by having its layout and datasource switched out during runtime)? I figure that it is pointing to the layout which was used to initialise the controller, thus the issue with the view trhing to use the old layout.

Foi útil?

Solução

I figured this one out.

Here is the code which allowed the controlled to be reused.

- (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;
        }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top