我有一个UICollectionView,它显示了一个json url的数组。将数组追加到UICollectionView
当用户滚动到collectionView结束时,我想追加另一个json url数组到我的collectionView。我这样做就像这样,但它没有工作。例如,collectionView中的项目是36,但在collectionView中,我在页面中间看到6个。我的代码:
- (void)loadMore {
NSString *path = @"https://example.com/?json=get_posts&page=2";
path = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSArray *array = json[@"posts"];
self.contentArray = [self.contentArray arrayByAddingObjectsFromArray:array];
NSLog(@"%ld",(long)self.contentArray.count);
[self.contentCollection reloadData];
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
NSInteger lastSectionIndex = [self.contentCollection numberOfSections] -1;
NSInteger lastRowIndex = [self.contentCollection numberOfItemsInSection:lastSectionIndex] -1;
if ((indexPath.section == lastSectionIndex) && (indexPath.row == lastRowIndex)) {
[self loadMore];
}
}
谢谢。
你的意思是 “loadMore” 功能从不叫? –
不,它叫,但没有工作的权利。见答案评论。 @AliOmari – user3767387
是你的代表吗? –