2016-11-27 161 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