Ravi
Ravi

Reputation: 848

UICollectionView layout crashes on iPad, but works fine on iPhone

UICollection view layout crasing in ipad, but works fine iphone. Will we have to manage UICollection view layout for ipad differently than iphone?

My code :

-(void)reloadData {


    if(collectionData!= nil)
    {
        collectionData = nil;
    }

    [self.collectionView reloadData];

}

-(void)setCollectionView {

    if(self.collectionView == nil) {
            //        self.collectionView = (UICollectionView *)[self.view viewWithTag:_wallCollectionView_tag];
       self.collectionView.dataSource = self;
        self.collectionView.delegate = self;
        self.collectionView.userInteractionEnabled = YES;
    }

}

`enter code here`- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{printf("\n = %s",__FUNCTION__);

    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

    return [collectionData count];
}

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout*)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if ( [ [ UIScreen mainScreen ] bounds ].size.width > 320) {
        return CGSizeMake(118, 118);
    }
    return CGSizeMake(100, 100);
}

Upvotes: 0

Views: 1544

Answers (1)

4esterUA
4esterUA

Reputation: 285

*** Terminating app due to uncaught exception 'NSRangeException', reason:     '*** -[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 4]' ***

This clearly says, that you trying to access element, which is beyond array bounds. Check next:

  1. You use same array for -numberOfItemsInSection method and for -cellForItemAtIndexPath method.
  2. You don't mutating array (deleting\adding elements) while collectionView is updating.
  3. You accessing datasource array correctly: to retrieve last element of array, which have 5 elements, you should use key "4" - collectionData[4] or collectionData[[collectionData count] - 1].

Upvotes: 1

Related Questions