ios UICollectionView 的 cellForItemAtIndexPath 未被调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14668781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:07:29  来源:igfitidea点击:

UICollectionView's cellForItemAtIndexPath is not being called

iosobjective-cuicollectionview

提问by IkegawaTaro

Only my second time using UICollectionView's and perhaps I have bitten off more than I can chew but nevertheless:

只是我第二次使用 UICollectionView 的,也许我已经咬掉了我无法咀嚼的东西,但是:

I am implementing a UICollectionView (myCollectionView) that uses custom UICollectionViewCell's that I have subclassed. The subclassed cells (FullReceiptCell) contain UITableView's and are the size of the viewcontroller. I am trying to allow for horizontal scrolling between the FullReceiptCells.

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

The subclassed UICollectionViewController that contains myCollectionView is being pushed on to a nav controller stack. Currently, myCollectionView loas and horizontal scrolling is enabled. However, no cells are visible. I have confirmed that

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

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

has run and is returning an integer greater than 0. I have also confirmed that myCollectionView's delegate and datasource are properly set in IB to the subclassed UICollectionViewController.

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

The method where the cells are to be loaded:

要加载单元格的方法:

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

is not being called.

没有被调用。

Here is where I push the UICollectionViewController and my viewDidLoad method within that controller (NOTE: initWithBill is an override of the normal initializer):

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

In the prior ViewControllers .m file:

在之前的 ViewControllers .m 文件中:

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

In FullReceiptViewController.m:

在 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];

Any clue as to why it is not being called?

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

回答by IkegawaTaro

For those who stumble here later.... the reason:

对于那些后来在这里绊倒的人......原因:

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

was not being called was because of the itemSize for the collectionViewFlowLayout's height was too big.

没有被调用是因为 collectionViewFlowLayout 高度的 itemSize 太大。

[self.myCollectionViewFlowLayout setItemSize:CGSizeMake(320, 548)];

If I change the height to 410, it will execute cellForItemAtIndexPath.

如果我将高度更改为 410,它将执行 cellForItemAtIndexPath。

回答by esilver

In my case, it was because my layout class incorrectly subclassed from UICollectionViewLayoutinstead of UICollectionViewFlowLayout

就我而言,这是因为我的布局类错误地从而UICollectionViewLayout不是从UICollectionViewFlowLayout

回答by Deepak G M

The cellForItemAtIndexPath will not get called if you do not provide the content size information to collection view.

如果您不向集合视图提供内容大小信息,则不会调用 cellForItemAtIndexPath。

  1. If you are using Flow layout: You need to set the item sizes properly.
  2. If you have a custom layout subclassed from UICollectionViewLayout: Ensure you are returning a proper size from the collectionViewContentSize method.
  1. 如果您使用 Flow 布局:您需要正确设置项目大小。
  2. 如果您有一个从 UICollectionViewLayout 子类化的自定义布局:确保从 collectionViewContentSize 方法返回正确的大小。

In case of the latter, you will also observe that your layoutAttributesForElementsRectis also not called. The reason is that you have not specified what is your content size and by default the size will be CGSizeZero. This basically tell collection view that you don't have any content to paint so it does not bother asking you for attributes or cells.

在后者的情况下,您还将观察到您的layoutAttributesForElementsRect也没有被调用。原因是您没有指定您的内容大小,默认情况下大小将为 CGSizeZero。这基本上告诉集合视图您没有任何要绘制的内容,因此它不会打扰您提供属性或单元格。

So, just override collectionViewContentSize and provide a proper size there, it should solve your problem.

因此,只需覆盖 collectionViewContentSize 并在那里提供适当的大小,它应该可以解决您的问题。

回答by Anthony Castelli

Maybe I'm just overlooking it, but it appears your missing your delegate and data source. In your header file, make sure you have added these:

也许我只是忽略了它,但您似乎缺少委托和数据源。在你的头文件中,确保你已经添加了这些:

<UICollectionViewDelegate, UICollectionViewDataSource>

and in your viewDidLoad method add this:

并在您的 viewDidLoad 方法中添加以下内容:

self.myCollectionView.delegate = self;
self.myCollectionView.dataSource = self;

Also, if you are loading it via an .xib, make sure you are have connected the IBOutlet to the UICollectionView.

此外,如果您通过 .xib 加载它,请确保您已将 IBOutlet 连接到 UICollectionView。

回答by Jaybo

For a more complicated view hierachy please check this blog. It saved my life!

有关更复杂的视图层次结构,请查看此博客。它救了我的命!

self.automaticallyAdjustsScrollViewInsets = NO;

回答by Gurjinder Singh

If your class is subclass from UICollectionviewController and you are creating collectionView programmatically then use

如果您的类是 UICollectionviewController 的子类,并且您正在以编程方式创建 collectionView,则使用

let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .Vertical
    layout.itemSize = CGSizeMake(50, 50)

    collectionView?.setCollectionViewLayout(layout, animated: false)

回答by AamirR

I was using autolayout, and in my case height constraint was missing

我正在使用自动布局,并且在我的情况下缺少高度约束

回答by Dean

In my case, what I had very stupidly forgotten to do was implement the UICollectionViewDataSourceprotocol method numberOfItemsInSection, so that is another thing to check.

就我而言,我非常愚蠢地忘记做的是实现UICollectionViewDataSource协议方法numberOfItemsInSection,所以这是另一件事要检查。

回答by nebyark

My issue was that I had 2 collection views within the same view, and both were sharing a computed UICollectionViewFlowLayoutinstance. Once I created separate instances of the same flow layout, it worked fine.

我的问题是我在同一个视图中有 2 个集合视图,并且都共享一个计算UICollectionViewFlowLayout实例。一旦我创建了相同流布局的单独实例,它就可以正常工作。

回答by sasha_nec

Also, make sure that reloadData is not called while collection view is updating with animation (insert, delete, update or performBatchUpdates).

此外,请确保在使用动画(插入、删除、更新或 performBatchUpdates)更新集合视图时不调用 reloadData。