UICollectionview cellForItemAtIndexPath not gettin

2019-09-01 07:20发布

问题:

I've been working my apps to run on iOS 8 and trying to consume data from the restful API. After getting response from the webservice I'm trying to reload the collectionview from the delegate method.I get the valid numeric count as 5 from my array for numberOfItemsInSection but cellForItemAtIndexPath not getting called.

I see in one scenario, If I hardcode the numberOfItemsInSection return count as 5 then cellForItemAtIndexPath is perfectly called while reload.

Here is my code snippet:

#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;   
}

Please help me on this. Quick replies are most appreciated. Thanks

回答1:

Finally I found an answer for my blocker. I just Reloading some items didn't work for me using reload data. Since CV default flow layout does some internal operation itself to reload data when we forced, In my case, I'm using customviewlayout here layoutAttributesForItemAtIndexPath and layoutAttributesForElementsInRect called only when the section gets loaded. Here it works because the collectionView has just one section, I've reloaded specific section would make contents correctly reloaded in the CV.

At my API handler delegate method placed the below code snippet,

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

Thanks.