could not dequeue a view of kind: UICollectionElementKindCell with identifier - ios

I have UICollectionView, but it seems that I set up everything right. But I get the error:
'could not dequeue a view of kind: UICollectionElementKindCell with identifier PeopleCellForList - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
My storybord:
My code is:
#interface PeopleCellForList : UICollectionViewCell
#end
#implementation PeopleCellForList
#end
#pragma mark - UICollectionView Datasource
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
return self.arrayPeople.count;
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:#"PeopleCellForList " forIndexPath:indexPath];
return cell;
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
}
#pragma mark – UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGFloat width = 106;
CGFloat height = width;
if (indexPath.row % 3 == 2) {
width = 108;
}
return CGSizeMake(width, height);
}
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0, 0, 0, 0);
}
I tried [self.collectionViewMain registerClass:[PeopleCellForList class] forCellWithReuseIdentifier:#"PeopleCellForList"] in viewDidLoad: (during that I tried to remove and not remove cell from storyboard), but that didn't help.

You have an extra space in #"PeopleCellForList "

Related

UICollectionViewCell With Dynamic Size Cell Spacing Issue

I'm Creating Custom UICollectionViewCell it's size automatically set from it's Content.
The Dynamic Size of cell is Working But In That It Have Extra Space.
So I Want to remove extra Space Between Two Cell
I'm also set UICollectionView DataSource and Delegate and Impalement It's Method But Still it is not Working
In One Row not fix their only 2 cell. It have 1, 2, or 3 cell it's depend on Text
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 = #[#"abcdef”,#"abcdef”,#"abcdef”,#"abcdef”,#"abcdef”];
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 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 8;
}
Try to add Dynamic Size of cell with following delegate method
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake((self.view.frame.size.width - 15)/2, (self.view.frame.size.width - 15)/2);
}

UICollectionView Assertion failure in createPreparedCellForItemAtIndexPath

my code is simple,just want to create a flow layout UIColloctionView
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 7;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [UICollectionViewCell new];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(SCREEN_WIDTH / 2 - 5, 160);
}
and this is the error
*** Assertion failure in -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:],
/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3600.9.1/UICollectionView.m:2115
Try using dequeue reusability of the collection view cell:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell * cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"cell" forIndexPath:indexPath];
return cell;
}
as the documentation said
the collection view requires that you always dequeue views, rather than create them explicitly in your code
this is different from UITableView , just deque the cell all the time
wrong:
UICollectionViewCell *cell = [UICollectionViewCell new];
return cell;

[UICollectionView performSelector:withObject:]: message sent to deallocated instance

I am having problem with my iOS app which I am not sure what is the root cause of it even after I've turned on NSZombie to debug.
Already tried to google with reliable source but still couldn't get any answer.
For my iOS App, I am using MMDrawerController Library as the left slide menu. Which I am having a problem when I switching view from left menu, if the page contains UICollectionView, it will crash with the message below.
[UICollectionView performSelector:withObject:]: message sent to deallocated instance
The view load smooth for the first time, but when I attempt to switch back from another view, it crashed with the error above.
Here are my UICollectionView Data Source and cell builder functions
#pragma mark: UICollectionViewCell Builder
- (ItemNormalCVCell *)buildItemNormalCVCell:(NSIndexPath *)indexPath
{
ItemNormalCVCell *cell = [self.mCollectionView dequeueReusableCellWithReuseIdentifier:#"CVCellItemNormal" forIndexPath:indexPath];
return cell;
}
- (LoadingCVCell *)buildLoadingCVCell:(NSIndexPath *)indexPath
{
LoadingCVCell *cell = [self.mCollectionView dequeueReusableCellWithReuseIdentifier:#"CVCellLoading" forIndexPath:indexPath];
return cell;
}
#pragma mark: UICollectionView DataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
NSInteger itemsCount = self.items.count;
if( self.isLoading_.boolValue )
return itemsCount + 1;
return itemsCount;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger itemsCount = self.items.count;
if( self.isLoading_.boolValue ){
if( indexPath.row == itemsCount )
return [self buildLoadingCVCell:indexPath];
}
return [self buildItemNormalCVCell:indexPath];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat cvWidth = (self.view.frame.size.width / 2) - 1.f;
return CGSizeMake(cvWidth, 300.f);
}
- (UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0, 0, 0, 1.f); // top, left, bottom, right
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 1.f;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 1.f;
}

iOS: Scroll one cell at a time

I have horizontal scrolling set up now I would like for it to sort of scroll/snap one cell at a time when scrolling side to side.
Could someone shed some light? Here's the code I have so far:
- (void)viewDidLoad
{
[super viewDidLoad];
}
#pragma mark - UICollectionView Datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 1;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 20;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = [cv dequeueReusableCellWithReuseIdentifier:#"MyCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(50, 20, 50, 20);
}
[collectionView setPagingEnabled:YES]; might do the trick?
http://adoptioncurve.net/archives/2013/04/creating-a-paged-photo-gallery-with-a-uicollectionview/

How to set UICollectionView inside UIView

I am trying to add UICollectionViewControllers inside UIView class.
I cant add the controls.
CGRect viewframes=CGRectMake(0,400,self.view.bounds.size.width,
self.view.bounds.size.height/2);
self.button=[[view2 alloc]initWithFrame:viewframes];
self.button.backgroundColor=[UIColor grayColor];
[self.view addSubview:self.button];
Add UICollectionView control to your xib and set outlet of that and also add it's delegate methods UICollectionViewDataSource and UICollectionViewDelegate to .h file .
Add below code to your .m file
#pragma mark Collection View Methods
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(10, 10, 10, 10);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [urlArray count];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(140, 140);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
GalleryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cell" forIndexPath:indexPath];
cell.tag=indexPath.row;
return cell;
}
If you are using custom cell and you want to reload Collection view than add below code
[YourCollectionview registerNib:[UINib nibWithNibName:#"GalleryCell" bundle:nil] forCellWithReuseIdentifier:#"cell"];
[YourCollectionview reloadData];

Resources