2

Has anyone ever gotten a PFQueryCollectionViewController to load images with Objective C?

When the user first loads the view it will load the text, but not the images. The second time the user loads the view, the images are loaded. I do not understand.

- (PFCollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
                                  object:(PFObject *)object {
    PFCollectionViewCell *cell = [super collectionView:collectionView cellForItemAtIndexPath:indexPath object:object];

    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.text = object[@"name"];
    cell.imageView.file = object[@"icon"];
    // If the image is nil - set the placeholder
    if (cell.imageView.image == nil) {
        cell.imageView.image = [UIImage imageNamed:@"Icon.png"];
        [cell.imageView loadInBackground];
   }
    cell.contentView.layer.borderWidth = 1.0f;
    cell.contentView.layer.borderColor = [UIColor clearColor].CGColor;

    return cell;


}
Mastertron
  • 45
  • 1
  • 6

1 Answers1

0

The behavior is consistent with the imageView.image being non-nil initially. Just perform the placeholder assignment unconditionally. Once the image is cached, the placeholder will be undone by the assignment of the real image (before anything is drawn)...

cell.imageView.image = [UIImage imageNamed:@"Icon.png"];
cell.imageView.file = object[@"icon"];
[cell.imageView loadInBackground];
danh
  • 62,181
  • 10
  • 95
  • 136
  • Thanks but i'm still getting the same result :/ – Mastertron Jan 04 '16 at 03:53
  • I found this but I didn't learn swift as yet http://stackoverflow.com/a/31270733/5742044 – Mastertron Jan 04 '16 at 04:30
  • The translation is that you must set a placeholder image. But your logic does that. Can you try logging the image after you set it? `NSLog(@"%@", cell.imageView.image);` – danh Jan 04 '16 at 05:58
  • 2016-01-04 12:33:00.748 TestApp[4414:169946] (null) 2016-01-04 12:33:00.750 TestApp[4414:169946] (null) 2016-01-04 12:33:00.751 TestApp[4414:169946] (null) 2016-01-04 12:33:00.751 TestApp[4414:169946] (null) 2016-01-04 12:33:00.752 TestApp[4414:169946] (null) 2016-01-04 12:33:00.753 AeroTV[4414:169946] (null) – Mastertron Jan 04 '16 at 16:34
  • That's after you set it? Strong evidence that `imageNamed:@"Icon.png"` doesn't exist, right? – danh Jan 04 '16 at 16:38
  • just placed Icon.png to the project and then it solved, thanks! – Mastertron Jan 04 '16 at 17:27