【问题标题】:UICollectionview cellForItemAtIndexPath not getting called when reload in iOS 8在 iOS 8 中重新加载时未调用 UICollectionview cellForItemAtIndexPath
【发布时间】:2015-08-15 03:44:23
【问题描述】:

我一直在努力让我的应用程序在 iOS 8 上运行,并尝试使用来自 RESTful API 的数据。从网络服务获得响应后,我尝试从委托方法重新加载集合视图。我从数组中为 numberOfItemsInSection 获取有效数字计数为 5,但未调用 cellForItemAtIndexPath。

我在一种情况下看到,如果我将 numberOfItemsInSection 返回计数硬编码为 5,那么在重新加载时完美调用 cellForItemAtIndexPath。

这是我的代码 sn-p:

#pragma mark = UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
     return 1; 
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
     return [offerModel.arrayOffers count]; 
}

- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView
 cellForItemAtIndexPath:(NSIndexPath*)indexPath {
     NSString *identifier = @"OfferCell";

     static BOOL nibMyCellloaded = NO;

     if(!nibMyCellloaded){
         UINib *nib = [UINib nibWithNibName:identifier bundle: nil];
         [offerCollectionView registerNib:nib forCellWithReuseIdentifier:identifier];
         nibMyCellloaded = YES;
     }

   OfferCell* cell = (OfferCell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier
 forIndexPath:indexPath]; //     // Commented binding values return
 cell; 
}

- (void)updateOfferList{
     [offerCollectionView reloadData]; 
}

-(void)viewDidLoad{
     [offerCollectionView registerClass:[OfferCell class] forCellWithReuseIdentifier:@"OfferCell"];

     // set up delegates
     self.offerCollectionView.delegate = self;
     self.offerCollectionView.dataSource = self;   
}

请帮助我。快速回复最受赞赏。谢谢

【问题讨论】:

  • 为什么不编辑您的问题以显示numberOfItemsInSection: 的代码?
  • 您确定“我从我的 numberOfItemsInSection 数组中获得的有效数字计数为 5”?你怎么知道的?
  • 我确实调试了数组计数并打印了计数,最初当视图加载计数时打印“0”,从 API 接收到对象后,计数打印并返回“5”。我很确定。
  • 专家对此问题有何更新?
  • 您确定您的collectoinView 是offercollectionview 类型吗?更进一步:你应该在你的 iboutlet 上设置委托,如果 collectionView 是你的实际 UI 项,那么你应该调用 self.collectoinView.reloadData

标签: ios objective-c uicollectionview


【解决方案1】:

我终于找到了我的拦截器的答案。我只是使用重新加载数据重新加载一些项目对我不起作用。由于 CV 默认流布局在我们强制时会执行一些内部操作以重新加载数据,就我而言,我在这里使用 customviewlayout layoutAttributesForItemAtIndexPathlayoutAttributesForElementsInRect 仅在该部分获取时调用加载。在这里它可以工作,因为 collectionView 只有一个部分,我重新加载了特定部分会使内容正确地重新加载到 CV 中。

在我的 API 处理程序委托方法中放置了以下代码 sn-p,

[self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];                 
[self.collectionView reloadData];

谢谢。

【讨论】:

  • 您在重新加载数据之前尝试过[self.collectionView.collectionViewLayout invalidateLayout] 吗?此外,请确保您在调用重新加载数据时位于主线程上。如果您不在 UI 线程上,则 CollectionView 不会按预期重新加载。
  • 是的,我按照你的建议做了同样的事情,但对我没有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-12
  • 1970-01-01
相关资源
最近更新 更多