私はUICollectionViewを持っており、json URLの配列を表示しています。配列をUICollectionViewに追加する
ユーザーがcollectionViewの最後までスクロールするときに、別のjson url配列をコレクションビューに追加したいとします。私はこのようにしますが、それは仕事をしませんでした。たとえば、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];
}
}
ありがとう。 self.contentArray
を再初期化しないでください。このaddObjectsFromArray
あなたが "loadMore" 機能することを意味しますか決して呼ばれない? –
いいえ、それはうまく動作しませんでした。答えのコメントを見てください。 @AliOmari – user3767387
はあなたの代理人ですか? –