2015-08-15 30 views
0

我一直在努力使我的應用程序在iOS 8上運行,並嘗試使用來自restful API的數據。從webservice獲取響應後,我試圖從委託方法重新加載collectionview。我從numberOfItemsInSection的數組中獲取有效的數字計數爲5,但cellForItemAtIndexPath沒有被調用。UICollectionview cellForItemAtIndexPath在iOS 8中重新加載時未得到調用

我在一個場景中看到,如果我硬編碼numberOfItemsInSection返回計數爲5,然後cellForItemAtIndexPath完全調用時重新加載。

這裏是我的代碼片段:

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

請幫我在這。快速回復非常感謝。謝謝

+0

爲什麼不編輯你的問題來顯示'numberOfItemsInSection:'的代碼? –

+0

你確定「我的數組中有效數字數爲5」numberOfItemsInSection「?你怎麼知道的? – anhtu

+0

我確實調試了數組數並打印計數,最初當視圖加載計數打印「0」後,從API接收對象的計數打印並返回「5」。我很確定。 – Sathish

回答

1

最後,我找到了我的阻滯劑的答案。我只是重新加載一些項目不適合我使用重新加載數據。由於CV默認流佈局自己做一些內部操作來強制重新加載數據,在我的情況下,我在這裏使用了customviewlayout layoutAttributesForItemAtIndexPathlayoutAttributesForElementsInRect只有在部分被加載時才調用。這裏它的工作原理是因爲collectionView只有一個部分,我重新加載了特定的部分會使內容在CV中正確地重新加載。

在我的API處理器的委託方法放在下面的代碼片段,

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

感謝。

+0

在重新加載數據之前,您是否嘗試過使用'[self.collectionView.collectionViewLayout invalidateLayout]'?另外,當您調用重新加載數據時,請確保您在主線程中。如果您不在UI線程上,CollectionView將不會按預期重新加載。 – jervine10

+0

是的,我做了同樣的推薦,但對我沒有幫助。 – Sathish

相關問題