1

I have check a lot of links regarding this but i could not come to a conclusion. What i want to do is have dynamic size for cells of a UICollectionView. I have an array of images with different sizes and i want to display them. I can't figure out why other answers on SO are so complicated for this problem, what i understand is, i just need to calculate the size of image in the sizeForItemAtIndexPath method as mentioned here but i am getting an error, *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString size]: unrecognized selector sent to instance 0x10fc860a0 My implementation for sizeForItemAtIndexPath is as,

- (CGSize)collectionView:(UICollectionView *)collectionView
              layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    UIImage *image = [arrRecipeImages objectAtIndex:indexPath.row];
    CGSize size = CGSizeMake(image.size.width, image.size.height);
    return size;
}

Everything looks fine to me, can someone point out where i am going wrong, thanks in advance.

Community
  • 1
  • 1

1 Answers1

0

Look carefully at the error : [__NSCFConstantString size]. It looks like you are trying to call the size method on a string object.

Are you sure that arrRecipeImages is made of images ? You explicitly cast your item from the array as an UIImage object, but it can be anything.

Michaël Azevedo
  • 3,874
  • 7
  • 31
  • 45
  • yes, code works fine and all images are displayed if i comment out sizeForItemAtIndexPath method. –  Feb 22 '16 at 10:09
  • Can you edit your question and add your `cellForItemAtIndexPath` method ? – Michaël Azevedo Feb 22 '16 at 10:11
  • 1
    Thanks for your hint, i changed the previous code with `UIImage *image = [UIImage imageNamed:[arrRecipeImages objectAtIndex:indexPath.row]];`, the error is gone but not all the images are displayed. –  Feb 22 '16 at 10:15