IOS UICollectionView insertItemsAtIndexPaths crash when header is on screen - ios

I have an UICollectionView with a header of size non-zero. It seems whenever insertItemsAtIndexPaths is called, if the header is on screen, the program crashes with the following message:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: <_UICollectionViewItemKey: 0xa38c910> Type = SV Kind = UICollectionElementKindSectionHeader IndexPath = <NSIndexPath 0xa38c7c0> 2 indexes [0, 0])'
When the header size is zero, or when the header is not on screen, calling insertItemsAtIndexPaths works fine. Does anyone know how to fix this? Thanks!
The class is a sub class of UICollectionViewController. Here is the code related to UICollectionView:
- (id)init {
// Collection view layout
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(100, 100);
layout.headerReferenceSize = CGSizeMake(320, 50); // Left for the switch
layout.minimumInteritemSpacing = 5;
layout.minimumLineSpacing = 5;
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
if (self = [super initWithCollectionViewLayout:layout]) {
// Collection view setup
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"ID"];
self.collectionView.frame = CGRectMake(0, -20, 320, 480-20-44-49);
self.collectionView.backgroundView = nil;
self.collectionView.backgroundColor = [UIColor clearColor];
...
}
return self;
}
Then I implemented two delegate methods:
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [blogs count];
}
and
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionViewArg cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionViewArg dequeueReusableCellWithReuseIdentifier:#"ID" forIndexPath:indexPath];
/* Some code to get blogView */
[cell.contentView addSubview:blogView];
return cell;
}

The problem is there said in log that the header cannot be nil.So give some valid input there and you can avoid the crash.
Like if the header section needs no input give it an view with clearclolor
For implementhing the header section you must implement the following datasource method
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;

Related

UICollectionViewCell With automatic sizing not Working in iOS 9 But Working in iOS 10

I want to make dynamic size of UICollectionViewCell.
It's Working on iOS 10 but app is crash in iOS 9.
I have tried many solutions but none works.
Screen Shot of iPhone 5s (Working)
I Want the above output on iOS 9
- (void)viewDidLoad {
[super viewDidLoad];
[self.collectionView setContentInset:UIEdgeInsetsMake(8, 8, 8, 8)];
[self.collectionView registerNib:[UINib nibWithNibName:#"CustomeCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:#"CustomeCollectionViewCell"];
UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
flowLayout.estimatedItemSize = CGSizeMake(1, 1);
flowLayout.minimumLineSpacing = 8.0f;
flowLayout.minimumInteritemSpacing = 8.0f;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CustomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"CustomeCollectionViewCell" forIndexPath:indexPath];
cell.lblText.text = [self.dataArr objectAtIndex:indexPath.item];
cell.backgroundColor = [UIColor blackColor];
return cell;
}
Error log
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' *** First throw call stack: (0x2592791b 0x250c2e17 0x2583972b 0x2a6faba7 0x2a6b8adf 0x2a698753 0x2a698d87 0x29f08d57 0x29f06ed3 0x29f01fe9 0x29e9ea73 0x27f36bcd 0x27f32375 0x27f32209 0x27f316d1 0x27f313a5 0x29e95b79 0x258e96c9 0x258e79cd 0x258e7dff 0x25837229 0x25837015 0x26e27ac9 0x29f0b189 0x66f2d 0x254df873) libc++abi.dylib: terminating with uncaught exception of type NSException
My issue is solved in that some change in Code and implement some method that is available in given code.
1) implement intrinsicContentSize method in CustomeCollectionViewCell
2) set value of tempCell from that get size in sizeForItemAtIndexPath method
CustomeCollectionViewCell.m
- (CGSize)intrinsicContentSize
{
CGSize size = self.lblText.intrinsicContentSize;
size.width += 48; // add padding Width that Given in Cell
size.height += 32; // add padding Height
return size;
}
ViewController.m
#interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
#property (strong, nonatomic) CustomeCollectionViewCell *tempCell;
#property (strong, nonatomic) NSArray *dataArr;
#end
- (void)viewDidLoad {
[super viewDidLoad];
self.dataArr = #[#"a",#"bc",#"def",#"ghijkl",#"mkopqrst",#"uvwxyz"];
self.tempCell = (CustomeCollectionViewCell*) [[[UINib nibWithNibName:#“CustomeCollectionViewCell" bundle:nil] instantiateWithOwner:self options:nil] firstObject];
[self.collectionView setContentInset:UIEdgeInsetsMake(8, 8, 8, 8)];
[self.collectionView registerNib:[UINib nibWithNibName:#"CustomeCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:#"CustomeCollectionViewCell"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CustomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"CustomeCollectionViewCell" forIndexPath:indexPath];
cell.lblText.text = [self.dataArr objectAtIndex:indexPath.item];
cell.backgroundColor = [UIColor blackColor];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
self.tempCell.lblText.text = [self.dataArr objectAtIndex:indexPath.item];
return self.tempCell.intrinsicContentSize;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 8;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 8;
}

Error : "could not dequeue a view of kind" in Objective-C

I Created a UICollectionViewController in StoryBoard and set it's class to CollectionViewController1 and i have a ReuseIdentifier.
Here is my error :
Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:
forIndexPath:viewCategory:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit
/UIKit-3600.8.1/UICollectionView.m:5115
Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind:
UICollectionElementKindCell with identifier Cell - must register a nib or a class
for the identifier or connect a prototype cell in a storyboard'
And it's my code :
#import "CollectionViewController1.h"
#interface CollectionViewController1 ()
{
UICollectionView *collectionview;
}
#end
#implementation CollectionViewController1
static NSString * const reuseIdentifier = #"Cell";
- (void)viewDidLoad {
[super viewDidLoad];
UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
layout.itemSize = self.view.frame.size;
collectionview = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
collectionview.pagingEnabled = TRUE;
collectionview.translatesAutoresizingMaskIntoConstraints = NO;
collectionview.delegate = self;
collectionview.dataSource = self;
collectionview.bounces = false;
[collectionview registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
[self.view addSubview:collectionview];
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
#warning Incomplete implementation, return the number of sections
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of items
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// Configure the cell
return cell;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
insetForSectionAtIndex:(NSInteger)section;
{
return UIEdgeInsetsMake(0, 0, 0, 0);
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return self.view.frame.size;
}
I think you have another UICollectionView in storyboard with empty cell identifier
Add self.collectionView = collectionview; after create collection view.
Your crash gives a different class to the one you register:
UICollectionElementKindCell
vs
UICollectionViewCell
Change one of these to the class you want - I'm guessing the latter.

UICollectionView not calling didSelectItemAtIndexPath

I have a Collection View and has custom cells with images and labels in there. I have set my collection view as follows -
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
flowLayout.minimumLineSpacing = 150.0f;
flowLayout.minimumInteritemSpacing = 104.0f;
flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 100, 120);
_archiveCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
_archiveCollectionView.frame = CGRectMake(30, 218, _archiveCollectionView.frame.size.width - 60, _archiveCollectionView.frame.size.height - 350);
_archiveCollectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_archiveCollectionView.backgroundColor = [UIColor clearColor];
_archiveCollectionView.delegate = self;
_archiveCollectionView.dataSource = self;
[self.archiveCollectionView registerNib:[UINib nibWithNibName:#"FullArchiveEditionCell" bundle:nil] forCellWithReuseIdentifier:#"MyCell"];
[_archiveCollectionView reloadData];
[self.view addSubview:_archiveCollectionView];
I have also set the following methods:
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return _chosenCategoryArray.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[self addEditionsChildView];
}
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
However, my didSelectItemAtIndexPath never gets called when I select a cell. Any help please?
I had a similar problem and it turned out I was using the same cell reuse identifier in two different collection views
In your header file have you implemented UICollectionViewDelegate as like below,
#interface HAViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate>
I have the same problem. And I solved by using storyboard to locate the collection cell in the collection view controller. Then tick User InterAction Enabled. I think using code to set in the UICollectionViewCell would also work. Hope it would help.
Try with changing sequence as given below
[_archiveCollectionView reloadData];
[self.view addSubview:_archiveCollectionView];
to
[self.view addSubview:_archiveCollectionView];
[_archiveCollectionView reloadData];
Implement the below delegate method.
- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
And implement your
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
//your selection management code
}
and
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
//deselection handling code
}

Custom UICollectionView of UIImages error

This is the first time I have ever tried to make a UICollectionView before, I have an ImageArray that is being read from coreData.. all of the images are NSData I am reading them into a UIImage... I would then like to display the UIImage into a UICollectionView that I allow the user to select from to update a preview view.
I have added these 3 delegates to my class.
And these are the delegates I have implemented.
// add collectionView
photoCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10.0, 50.0, 200.0, 700.0)];
[photoCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"PhotoCell"];
photoCollectionView.dataSource = self;
photoCollectionView.delegate = self;
[self.view addSubview:photoCollectionView];
//..
#pragma mark - CollectionView Delegates
#pragma mark -- UICollectionView Datasource
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
return [imageArray count];
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:#"PhotoCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
#pragma mark -- UICollectionView Delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
// TODO: Select Item
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
// TODO: Deselect item
}
#pragma mark –- UICollectionViewDelegate FlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *currentPhotoDict = [imageArray objectAtIndex:indexPath.row];
UIImage *imageForCollection = [UIImage imageWithData:[currentPhotoDict objectForKey:#"DImage"]];
//show image in collectionview?
}
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(50, 20, 50, 20);
}
When I run this code above I am receiving the following error.
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'
The error is stating that you need to create the collection view with a non-nil layout object.
You need to use...
photoCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10.0, 50.0, 200.0, 700.0) collectionViewLayout:someLayoutObject];
You need to create the layout object first too.
Possibly just use...
photoCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10.0, 50.0, 200.0, 700.0) collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
You need to specify kind of layout you want to use when you initialise collection view:
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
photoCollectionView =[[UICollectionView alloc] initWithFrame:CGRectMake(10.0, 50.0, 200.0, 700.0) collectionViewLayout:layout];

UICollectionView not recognized in collectionView numberOfItemsInSection:

I have a problem I can't solve for some hours now...
I have multiple UICollectionViews with different numbers of cells and different cellsizes. The CollectionViews are created programmatically and the delegates and datasources are set.
The collectionviews are created like this:
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
collectionViewOne = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0,320,150) collectionViewLayout:layout];
[collectionViewOne setTag:99];
[collectionViewOne setDataSource:self];
[collectionViewOne setDelegate:self];
[collectionViewOne registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"cell"];
[collectionViewOne setBackgroundColor:bgColor];
[collectionViewOne setShowsHorizontalScrollIndicator:NO];
[collectionViewOne setBounces:YES];
[collectionViewOne setAlwaysBounceHorizontal:YES];
[collectionViewOne setScrollEnabled:YES];
[collectionViewOne setRestorationIdentifier:#"collectionViewOne"];
[scrollView addSubview:collectionViewOne];
My functions are like these:
- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section {
NSLog(#"restorationIdentifier: %#", collectionView.restorationIdentifier);
if (collectionView == collectionViewOne)
{
return 3;
}
else if (collectionView == collectionViewTwo)
{
return 4;
}
else
{
return 1;
}
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"restorationIdentifier: %#", collectionView.restorationIdentifier);
if (collectionView == collectionViewOne)
{
return CGSizeMake(100, 150);
}
else if (collectionView == collectionViewTwo)
{
return CGSizeMake(200, 150);
}
else
{
return CGSizeMake(200, 150);
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"restorationIdentifier: %#", collectionView.restorationIdentifier);
if (collectionView == collectionViewOne)
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
imageView.image = [UIImage imageNamed:#"image.png"];
[cell addSubview:imageView];
}
else if (collectionView == collectionViewTwo)
{
//create cell of type 2
}
return cell;
}
In my log I get the following output (for example):
restorationIdentifier in numberOfItemsInSection: (null)
restorationIdentifier in sizeForItemAtIndexPath : (null)
restorationIdentifier cellForItemAtIndexPath: collectionViewOne
collectionViewOne is the restorationIdentifier on collectionViewOne. So why is it not recognized in the first two methods?
Ofcourse the result is that I get crashes because the cellsizes and number of cells in the different CollectionViews are not properly set. The error:
*** Assertion failure in -[UICollectionViewData validateLayoutInRect:], /SourceCache/UIKit_Sim/UIKit-2903.2/UICollectionViewData.m:341
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView recieved layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0x8c31c10> {length = 2, path = 0 - 2}'
How can I fix this?
This is solved. The problem was the fact that I used the same UICollectionViewFlowLayout for both UICollectionViews. This caused the exception.
In the end I used different classes for the two controllers, this solved the problem of selecting the right CollectionView in the delegate's methods.
Thanks all for your input!
You could use the tag property of the collection view to identify which collection view is which. Like this:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"restorationIdentifier: %#", collectionView.restorationIdentifier);
if (collectionView.tag == collectionViewOneTag)
{
//create cell of type 1
}
else if (collectionView.tag == collectionViewTwoTag)
{
//create cell of type 2
}
return cell;
}
Where collectionViewOneTag and collectionViewTwoTag are integers that can be defined in code or in the xib file.
Hope that helps.

Resources