【问题标题】:UICollectionView's cellForItemAtIndexPath is not being calledUICollectionView 的 cellForItemAtIndexPath 没有被调用
【发布时间】:2013-01-18 02:13:11
【问题描述】:

这只是我第二次使用 UICollectionView,也许我已经咬得比我能咀嚼的多,但是:

我正在实现一个 UICollectionView (myCollectionView),它使用我已经子类化的自定义 UICollectionViewCell。子类单元格 (FullReceiptCell) 包含 UITableView,并且是 viewcontroller 的大小。我正在尝试允许在 FullReceiptCells 之间进行水平滚动。

包含 myCollectionView 的子类 UICollectionViewController 被推送到导航控制器堆栈。目前,myCollectionView loas 和水平滚动已启用。但是,看不到任何单元格。我已经确认了

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

已运行并返回大于 0 的整数。我还确认 myCollectionView 的委托和数据源已在 IB 中正确设置为子类 UICollectionViewController。

要加载单元格的方法:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

没有被调用。

这是我在该控制器中推送 UICollectionViewController 和 viewDidLoad 方法的地方(注意:initWithBill 是普通初始化程序的替代):

在之前的 ViewControllers .m 文件中:

FullReceiptViewController *test = [[FullReceiptViewController alloc] initWithBill:currentBill];
test.title = @"Review";
[self.navigationController pushViewController:test animated:YES];

在 FullReceiptViewController.m 中:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.myCollectionView registerClass:[FullReceiptCell class] forCellWithReuseIdentifier:@"FullReceiptCellIdentifier"];
    self.myCollectionView.pagingEnabled = YES;
    // Setup flowlayout

    self.myCollectionViewFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    [self.myCollectionViewFlowLayout setItemSize:CGSizeMake(320, 548)];
    [self.myCollectionViewFlowLayout setSectionInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    [self.myCollectionViewFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    self.myCollectionViewFlowLayout.minimumLineSpacing = 0;
    self.myCollectionViewFlowLayout.minimumInteritemSpacing = 0;
    [self.myCollectionView setCollectionViewLayout:myCollectionViewFlowLayout];
    //testing to see if the collection view is loading
    self.myCollectionView.backgroundColor = [UIColor colorWithWhite:0.25f alpha:1.0f];

关于为什么它没有被调用的任何线索?

【问题讨论】:

  • 我正在使用 XIB。我无法将 cellForItemsAtIndexPath 中的任何内容出列,因为它永远不会被调用。

标签: ios objective-c uicollectionview


【解决方案1】:

i) 请确保将 UICollectionView 添加到 UITableViewCells contentView 并且不要作为 subView

【讨论】:

    【解决方案2】:

    我花了一些时间来处理从情节提要加载的包含视图控制器中的 CollectionView 出现的问题。我最终只是删除了包含的视图控制器,并在情节提要中重新创建它,这似乎解决了这个问题。我的猜测是发生了某种情节提要损坏。

    【讨论】:

      【解决方案3】:

      在自定义单元格的 layoutSubViews() 中设置 collectionView 框架对我有用:

      override func layoutSubviews() {
              super.layoutSubviews()
              let frame = self.contentView.bounds
              self.CollectionView.frame = frame
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-17
        • 1970-01-01
        • 1970-01-01
        • 2016-06-16
        • 1970-01-01
        • 1970-01-01
        • 2013-07-16
        • 1970-01-01
        相关资源
        最近更新 更多