【发布时间】:2020-01-05 22:06:06
【问题描述】:
我试图在我的 UIViewController 中有一个 UICollectionViewController。 这是我的设置方式
TagsCollectionViewController* tagsCollectionViewController = [[TagsCollectionViewController alloc] initWithCollectionViewLayout:[UICollectionViewLayout new]];
UIView *tagsView = tagsCollectionViewController.view;
[self.view addSubview:tagsView]; // setting up constraings too
这是我的TagsCollectionViewController
@interface TagsCollectionViewController () <UICollectionViewDelegateFlowLayout>
@end
@implementation TagsCollectionViewController
static NSString * const reuseIdentifier = @"Cell";
- (void)viewDidLoad {
[super viewDidLoad];
self.collectionView.backgroundColor = [UIColor yellowColor];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 20;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
return cell;
}
#pragma mark <UICollectionViewDelegateFlowLayout>
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(50, 50);
}
@end
奇怪的是,我看到黄色视图,所以我的收藏似乎在工作,但是我看不到任何项目。但是当我展示整个控制器时,一切正常。所以问题只有在我尝试嵌入它时才会出现。可能是什么问题?
编辑:相关:Adding UICollectionViewController to UIViewController Not Working
【问题讨论】:
-
您是否保留了对您的
TagsCollectionViewController的有效引用,以便它可以充当委托/数据源? -
@PhillipMills 我现在使它成为一个强大的属性,并在 viewDidLoad 中对其进行了初始化。这是你的意思吗?不过还是不行。
-
它的方法是否被调用,例如项目数量? (也许放一个断点来看看你返回了什么。)
-
是的,它被调用并且作为一个单独的控制器工作得很好。它可能与我嵌入它的方式有关。
-
没关系,但我怀疑
cellForItemAtIndexPath根本没有被调用。
标签: ios objective-c