Cyril
Cyril

Reputation: 2818

UICollectionViewController Issue with Parse

I have data on my Parse database and I am trying to retrieve that data and show into a UILabel under a UICollectionViewCell.

-(void)viewDidLoad {
[super viewDidLoad];
 PFQuery *retrieveClass = [PFQuery queryWithClassName:@"ClassName"];
    [retrieveClass findObjectsInBackgroundWithBlock:^(NSArray *objets, NSError *error) {

        if (!error) {
           classArray = [[NSArray alloc] initWithArray:objets];
            NSLog(@"%@", objets);
        }
    }];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}


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

    return classArray.count;
}

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

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

    PFObject *tempDict = [classArray objectAtIndex:indexPath.row];

    cell.label.text = [tempDict objectForKey:@"Name"];
    NSLog(@"%@",[tempDict objectForKey:@"Name"]);

    return cell;
}

classArray is an NSArray and I created a UICollectionViewCell file and created a UILabel then imported into my main file.

I added the cell identifier, made the connections and I don't know what is wrong.

The classnames are right, the field names are right, the parse ID's and ClientKeys are right.

I am getting log feedback under the query block but nothing under the UICollectionViewCell.

Log output enter image description here

Upvotes: 0

Views: 26

Answers (2)

Manann Sseth
Manann Sseth

Reputation: 2745

Try with reload data

[retrieveClass findObjectsInBackgroundWithBlock:^(NSArray *objets, NSError *error) {

    if (!error) {
       classArray = [[NSArray alloc] initWithArray:objets];
       NSLog(@"%@", objets);
       [self.collectionView reloadData];
    }
}];

Still you'll get any updated values, then call it with dispatch_async block.

dispatch_async(dispatch_get_main_queue(), ^{

    [self.collectionView reloadData];
});

Label value will be cell.label.text = [tempDict objectForKey:@"Name"];

Hopefully, it'll help you.
Thanks

Upvotes: 0

bunty
bunty

Reputation: 629

Change this line

cell.label.text = [classArray objectAtIndex:indexPath.row];

with

cell.label.text = [tempDict objectForKey:@"Name"];

Upvotes: 1

Related Questions