【问题标题】:How do change the data and layout in a collectionview?如何更改集合视图中的数据和布局?
【发布时间】:2014-04-25 01:19:23
【问题描述】:

我有一个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 是一个只读属性。这是否意味着控制器不打算以这种方式重用? (通过在运行时切换其布局和数据源来重用)?我认为它指向用于初始化控制器的布局,因此使用旧布局的视图存在问题。

【问题讨论】:

    标签: ios objective-c uicollectionview uicollectionviewlayout


    【解决方案1】:

    我想出了这个。

    这是允许重用控件的代码。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多