简体   繁体   中英

NSMutableArray can't add object from JSONModelArray?

I have a NSMutableArray and adding objects to it as below:

// Do any additional setup after loading the view.
array = [[NSMutableArray alloc] init];

[array addObject:@"Apple"];

[array addObject:@"Orange"];

[array addObject:@"Mango"];

[array addObject:@"Banana"];

But when I try to add object from JSONModelArray still showing the above 4 object only.

Here is JSONModelArray code:

//fetch the feed
_collectionBusinessFeeds = [[CollectionBusinessManager alloc] initFromURLWithString:@"http://localhost:8888/json/collectionBusinessFinal.json"
                           completion:^(JSONModel *model, JSONModelError *err) {


                               for(NSDictionary *item in _collectionBusinessFeeds.collectionBusinesses){
                                   NSLog(@"%@",item);
                                   NSString *title = [item valueForKey:@"name"];

                                   [array addObject:title];

                                   NSLog(@"%@",array);
  // here array is printing all the objects with the above 4 objects

                               }

                          }];

But return array here below only showing above 4 objects not the all the objects added from JSONModelArray.

-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

return [array count];
// only showing 4 above obects like Apple, Banana...
}

Here is my full implementation file:

    #import "CollectionViewController.h"
#import "AccordionViewController.h"

#import "JSONModelLib.h"
#import "CollectionBusinessManager.h"
#import "HUD.h"

@interface CollectionViewController (){

    CollectionBusinessManager* _collectionBusinessFeeds;
}

@end

@implementation CollectionViewController

NSMutableArray* array;

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    array = [[NSMutableArray alloc] init];

    [array addObject:@"Apple"];

    [array addObject:@"Orange"];

    [array addObject:@"Mango"];

    [array addObject:@"Banana"];

    //show loader view

    [HUD showUIBlockingIndicatorWithText:@"Fetching JSON"];

    //fetch the feed
    _collectionBusinessFeeds = [[CollectionBusinessManager alloc] initFromURLWithString:@"http://localhost:8888/json/collectionBusinessFinal.json"
                               completion:^(JSONModel *model, JSONModelError *err) {
                                //hide the loader view
                                [HUD hideUIBlockingIndicator];

                                   [self.v reloadData];

                                   NSLog(@"%@", _collectionBusinessFeeds.collectionBusinesses);
                                   for(NSDictionary *item in _collectionBusinessFeeds.collectionBusinesses){
                                       NSLog(@"%@",item);
                                       NSString *title = [item valueForKey:@"name"];

                                       [array addObject:[title copy]];

                                       NSLog(@"%@",array);

                                   }

                              }];




}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





#pragma mark collection view methods
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}


-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return [array count];
}


- (UIColor*) randomColor{
    int r = arc4random() % 255;
    int g = arc4random() % 255;
    int b = arc4random() % 255;
    return [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1];
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {



    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    UILabel *label = (UILabel *) [cell viewWithTag:100];
    label.text = [array objectAtIndex:indexPath.row];

    cell.backgroundColor = [self randomColor];

    return cell;


}


-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{

    AccordionViewController *detailVC = [[AccordionViewController alloc] init];
    detailVC.passParam= [array objectAtIndex:indexPath.row];
    detailVC.title = detailVC.passParam;

    [self.navigationController pushViewController:detailVC animated:YES];
}





/*


 #pragma mark - Navigation





 // In a storyboard-based application, you will often want to do a little preparation before navigation


 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {


 // Get the new view controller using [segue destinationViewController].


 // Pass the selected object to the new view controller.


 }


 */

@end

can anybody help me?

Update: at last it solved my problem adding the property like below:

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

[self.collectionView reloadData];

//fetch the feed
_collectionBusinessFeeds = [[CollectionBusinessManager alloc] initFromURLWithString:@"http://localhost:8888/json/collectionBusinessFinal.json"
                           completion:^(JSONModel *model, JSONModelError *err) {


                               for(NSDictionary *item in _collectionBusinessFeeds.collectionBusinesses){
                                   NSLog(@"%@",item);
                                   NSString *title = [item valueForKey:@"name"];

                                   [array addObject:title];

                                   [collectionView reloadData];

                                   NSLog(@"%@",array);

                               }

                          }];
I have edited code, please check above code.

For what you are describing, it seems like the fetch is being called after the collection view has calculated the number of items. You have to reload the collection view after fetching your new results.

UPDATED

    #import "CollectionViewController.h"
    #import "AccordionViewController.h"

    #import "JSONModelLib.h"
    #import "CollectionBusinessManager.h"
    #import "HUD.h"

    @interface CollectionViewController (){

        CollectionBusinessManager* _collectionBusinessFeeds;
    }
    @property (nonatommic, strong) NSMutableArray *array;
    @end

    @implementation CollectionViewController


    - (void)viewDidLoad {
        [super viewDidLoad];

        // Do any additional setup after loading the view.
        self.array = [[NSMutableArray alloc] init];
        [self.array addObject:@"Apple"];

        [self.array addObject:@"Orange"];

        [self.array addObject:@"Mango"];

        [self.array addObject:@"Banana"];

        But when I try to add object from JSONModelArray still showing the above 4 object only.

        Here is JSONModelArray code:

        //fetch the feed
        __weak typeof(self) weakSelf = self;
        _collectionBusinessFeeds = [[CollectionBusinessManager alloc]                                            initFromURLWithString:@"http://localhost:8888/json/collectionBusinessFinal.json"
                               completion:^(JSONModel *model, JSONModelError *err) {


                           for(NSDictionary *item in _collectionBusinessFeeds.collectionBusinesses){
                               NSLog(@"%@",item);
                               NSString *title = [item valueForKey:@"name"];

                               [weakSelf.array addObject:title];

                               NSLog(@"%@", weakSelf.array);
  // here array is printing all the objects with the above 4 objects

                           }

                      }];

Old Answer If you add __block to your array initialisation it must works.

        // Do any additional setup after loading the view.
        __block NSMutableArray *array = [[NSMutableArray alloc] init];
        [array addObject:@"Apple"];

        [array addObject:@"Orange"];

        [array addObject:@"Mango"];

        [array addObject:@"Banana"];

    But when I try to add object from JSONModelArray still showing the above 4 object only.

        Here is JSONModelArray code:

        //fetch the feed
        _collectionBusinessFeeds = [[CollectionBusinessManager alloc]                                            initFromURLWithString:@"http://localhost:8888/json/collectionBusinessFinal.json"
                               completion:^(JSONModel *model, JSONModelError *err) {


                           for(NSDictionary *item in _collectionBusinessFeeds.collectionBusinesses){
                               NSLog(@"%@",item);
                               NSString *title = [item valueForKey:@"name"];

                               [array addObject:title];

                               NSLog(@"%@",array);
  // here array is printing all the objects with the above 4 objects

                           }

                      }];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM