【问题标题】:objective-c Embedded UICollectionViewController shows 0 itemsobjective-c 嵌入式 UICollectionViewController 显示 0 项
【发布时间】: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


【解决方案1】:

估计集合中项目的大小似乎有问题。在创建了一个类似的示例并注意到没有请求单元格之后,我能够使用以下代码获得结果。

@interface ViewController ()
@property (nonatomic, strong) TagsCollectionViewController *tags;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
    layout.itemSize = CGSizeMake(50.0, 50.0);
    self.tags = [[TagsCollectionViewController alloc] initWithCollectionViewLayout:layout];
    UIView *tagsView = self.tags.view;
    [self.view addSubview:tagsView];
}

【讨论】:

  • 非常感谢!老实说,我不太确定如何,但它有效!
  • 也不确定。也许当您直接展示控制器时,它会应用一些默认大小,如果没有,它决定不要求单元格,因为它不知道如何显示它们。 (只是猜测。)
  • 即使没有 itemSize,它实际上也可以工作,我几乎可以肯定我尝试了确切的版本,但当时它并没有工作,呵呵
  • 我用 UICollectionViewLayout 而不是 UICollectionViewFlowLayout 来初始化它...
  • 是的,我想我给了你正确的答案,但推理错误。 :)
猜你喜欢
  • 2011-03-29
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 2013-01-04
  • 2018-06-03
相关资源
最近更新 更多