Bad Access on [UICollectionView setCollectionViewLayout:animated:] Bad Access on [UICollectionView setCollectionViewLayout:animated:] ios ios

Bad Access on [UICollectionView setCollectionViewLayout:animated:]


First, drag and drop a UICollectionView to your ViewController in XIB, hook up Delegate, datasource to ViewController(this is only main ViewController)

Don't use 2 different nib cell for 1 CollectionView, because you can only register 1 Nib. Better use DecorationView as HeaderView. Create new class HeaderView : UICollectionReusableView. This UICollectionReusableView is subclass of UIView can reuse inside UICollectionView together with UICollectionViewCell. Now you can register both type:

[self.collectionView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellWithReuseIdentifier:@"CELL"];[self.collectionView registerNib:[UINib nibWithNibName:@"HeaderView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderCell"];

Next, you drag another UICollectionView to this HeaderView, hook up with IBOutlet inside HeaderView.h. Here, it's better to set Delegate, DataSource to this class for control. Also register what Nib Cell this CollectionView will use. Do it in awakeFromNib because you registerNib before

- (void)awakeFromNib{    [self.topCollectionView registerNib:[UINib nibWithNibName:@"TopCell" bundle:nil] forCellWithReuseIdentifier:@"TopCell"];    [self.topCollectionView setDataSource:self];    [self.topCollectionView setDelegate:self];}

It's will work well, if you store your datasource outside, just make another property and assign to it, then use for return inside datasource here.

If you want to know when click on Cell inside headerView, Use a customDelegate protocol to send delegate to ViewController when click on HeaderCell.

This is my code, hope you understand and can apply your data here:

https://github.com/lequysang/gitfiles02/blob/master/CollectionViewWithDecorationView.zip


Looks like your delegate or inner collection view is dead(what does setWeakDelegatePointer: do?). Try Zombies instrument on simulator, it should flag if zombies are the case. Also set "Exceptions breakpoint" for all exceptions in xCode(will aid your debugging when app is run from xCode and not instruments). Also check your -[BSCFrontPageViewController collectionView:cellForItemAtIndexPath:] implementation, it may be releasing that inner collection view on reuse.

Edit: Why not just add header cell as header and not the cell 0(example of header in collection view here)? Also check(just to make sure it's not a reason of crash) your reuse identifiers when you are creationg normal cells for outer collection view. Also send mem warnings periodically when debugging on simulator.

Also, why use another collection view for header? You can use something similar to iCarousel for such layout.


Can you try the following, scroll the main uicollectionview so the header is not showing... (further better) then on simulator try to perform a memory warning... using Hardware->Simulate memory warning...

This should probably create a cell recycling and deletion (the uicollectionview should destroy anything that is not important right now... if this happens, the header will become deallocated and you still hold a weak reference to it... so anything that you do will happens because of bad access... also this is "imposible" to reproduce on the simulator due to lack of memory warning (only the ones that you create in a explicit way)..

The view looks heavy so can you try doing this and post the results?


matomo