我有一个UICollectionView,它显示了一个json url的数组。
当用户滚动到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];
}
}
谢谢。
答案 0 :(得分:0)
SELECT id,
datum,
stanjekm,
tocenolit,
stanjekm - lag (stanjekm,1,0) over (order by id) as PredjenoKm
FROM Gorivo as
//对于数组使用此addObjectsFromArray
答案 1 :(得分:0)
不要重新初始化self.contentArray
。
做[self.contentArray addObjectsFromArray:array];
它会将新对象附加到旧数组。
但请确保contentArray
是NSMutableArray
。
答案 2 :(得分:0)
好的我用以下代码解决了这个问题:
在我的ViewController.h中:
@property (assign, nonatomic) int page;
@property (strong, nonatomic) NSArray *contentArray;
@property (strong, nonatomic) IBOutlet UICollectionView *contentCollection;
在我的ViewController.m中:
- (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.item == lastRowIndex)) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *path = [NSString stringWithFormat:@"http://example.com/?json=get_posts&page=%d",self.page];
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);
dispatch_async(dispatch_get_main_queue(), ^ {
[self.contentCollection reloadData];
self.page = self.page + 1;
});
});
}}