2016-11-27 116 views
0

我有一个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]; 
    } 
} 

谢谢。

+0

你的意思是 “loadMore” 功能从不叫? –

+0

不,它叫,但没有工作的权利。见答案评论。 @AliOmari – user3767387

+0

是你的代表吗? –

回答

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; 
     }); 
    }); 
}} 
0
self.contentArray = [NSMutableArray array]; 
NSArray *array = json[@"posts"]; 
[self.contentArray addObjectsFromArray: array]; 
NSLog(@"%ld",(long)self.contentArray.count); 

//对于数组使用此addObjectsFromArray

+0

不,我不想删除第一项。对于FaceBook或Instagram示例,当您滚动到页面结尾时,会显示新项目。 – user3767387

0

不要重新初始化self.contentArray

只要做[self.contentArray addObjectsFromArray:array]; 它会将新对象追加到旧数组中。 但请确保contentArrayNSMutableArray