我在與的CollectionView willDisplayCell叫了兩聲
-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{}
我用這個方法來確定最後一個單元格將顯示,然後發出一個網絡請求拉更多的數據的問題。直到我從最後一排(當我BOOLS之一是假的)發出請求再次能正常工作如:
-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.item = [self.collectionView.datasource count]-1 && self.moreVendorsAvaiable) {
[network request:^(id response,id error){
if (!error){
[self processResponse:response];
}
}];
}
}
- (void) parseMoreVendors:(id)response{
//If response is > 0, there are more vendors to retrieve
NSLog(@"response");
if ([response count]>0) {
id parsedObject = nil;
NSMutableArray *vendorsArray = [NSMutableArray arrayWithArray:self.datasource.content[@"rows"];
NSMutableArray *updates = [[NSMutableArray alloc]init];
FeaturedHeader *header = self.datasource.content[@"header"];
Vendor *firstFeatured = header.firstFeatured;
Vendor *secondFeatured = header.secondFeatured;
for (id object in response) {
//check if the response object is a featured vendor and return if it is
if ([firstFeatured.vendorId isEqual:object[@"id"]] || [secondFeatured.vendorId isEqual:object[@"id"]]){
self.featuredReturned = YES;
return;
}
parsedObject = [[Vendor alloc] initWithDictionary:object];
[updates addObject:parsedObject];
}
[vendorsArray addObjectsFromArray:updates];
NSDictionary *content = [[NSDictionary alloc] initWithObjectsAndKeys:header, @"header", vendorsArray, @"rows", nil];
[self.datasource setContent:content];
[self.collectionView reloadData];
}else{
self.moreVendorsAvailable = NO;
}
}
當數據被檢索和數據源更新我打電話的CollectionView reloadData 。數據被重新加載,並且由於我處於底部單元格中,網絡請求再次發生,然後再次提取相同的數據。我如何防止它再次發出網絡請求?
可能你能夠分享整個班級代碼??? –
@muhammadRaheelMateen我已經添加了處理網絡請求 – Pablo