UICollectionView appears blank - cells don't show - ios

I've setup a UICollectionView with a custom cell XIB. I've setup the delegate, data source methods appropriately but upon loading the collection view appears blank with a black screen:
Here is my code for registering the custom xib cell:
[self.collectionView registerClass:[CustomViewCell class] forCellWithReuseIdentifier:#"cvCell"];
Delegate/data source methods:
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 2;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 4; }
-(CustomViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"cvCell";
CustomViewCell *cell = (CustomViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
return cell;
}
I've also set the class for the custom xib cell:
EDIT:
Collection view layout is set to:
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(50, 29)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self.collectionView setCollectionViewLayout:flowLayout];
The 50,29 refer to the size of the actual custom view xib cell

You should register your custom cell's nib instead of class.
UINib *cellNib = [UINib nibWithNibName:#"cvCell" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:#"cvCell"];

Related

black window in simulator -collection view

The code is a simple collection view with a bunch of images
I ran it and Xcode doesn't argue about anything but the simulator shows a black screen after running the app.
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return recipePhotos.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier =#"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
// Configure the cell
UIImageView * recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image=[UIImage imageNamed:[recipePhotos objectAtIndex:indexPath.row]];
return cell;
}
Check collection view datasource outlet, maybe it was not connected with your controller?
Check receipeImageView is not nil.
Did you register your UICollectionViewCell with identifier #"Cell"?
If you don't want black background, you have to set
collectionView.backgroundColor = [UIColor clearColor];
Then you must register the cell
UINib *cellNib = [UINib nibWithNibName:#"yournibname" bundle:nil];
[collectionView registerNib:cellNib forCellWithReuseIdentifier:#"cell"];
Both need to be implemented in viewDidLoad method

use of undeclared identifier collectionView

i am trying to create collectionView but it creates error as:
use of undeclared identifier in collectionView.
- (UICollectionView *)collectionView:(UICollectionView *)collectionView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell =
[collectionView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
if (cell == nil) {
cell = [[UICollectionViewCell alloc] init];
[UICollectionView setBackgroundColor:[UIColor redColor]];
}
[self.view addSubview:__collectionView];
// Do any additional setup after loading the view, typically from a nib.
}
If you are not using prototypeCell in the storyboard. You could either register Cell/Xib with the collectionView. Do this in viewDidLoad
[self.collectionView registerClass:[UICollectionViewCell class]
forCellWithReuseIdentifier: #"MyIdentifier"];
Registe your nib in view Did load method...
UINib *cellNib = [UINib nibWithNibName:#"NibCell" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:#"cvCell"];
now run your app...

iOS UICollectionView: Cells not showing

I'm missing something obvious here as this isn't a difficult task.
I have my Collection view in storyboard (amongst other views), with the prototype cell's reuse id being "Cell", and a UILabel in that cell, with a tag of 100.The code (boilerplate):
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 4;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
UILabel *label = (UILabel *)[cell viewWithTag:100];
label.text = #"Hello";
return cell;
}
The code is within a UICollectionViewController, as it should.
When i run, the view appears yet no cells or text are visible.
A NSLog in the cellForItemAtIndexPath confirms that it is called, 4 times.
I've even changed the prototype cell's background color which shows that not even the cell's are appearing.
In the viewDidLoad, it calls: [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
What am i missing?
Use like this:
Instead of registerClass: use registerNib:
static NSString *identifier = #"Cell";
if ([[UIDevice currentDevice] userInterfaceIdiom]==UIUserInterfaceIdiomPhone) {
[self.collection registerNib:[UINib nibWithNibName:#"CollectionPhotoItem" bundle:nil] forCellWithReuseIdentifier:identifier];
}
else{
[self.collection registerNib:[UINib nibWithNibName:#"CollectionPhotoItem_ipad" bundle:nil] forCellWithReuseIdentifier:identifier];
}
make sure you are under any-any ratio in your storyboard
if you are working with any other configuration and you tried to use the simulation it might not show up
for example if you use regular width and compact hight and then tried to use the simulator in your Xcode all your work will not show up !

Layout of Images in UICollectionView

I am trying to layout a collection of Magazine covers in a UICollectionView in which the background is a bookshelf. I want it to have 2 magazines per shelf, and the rest to be seen when scrolling down. Here is my code I have:
-(void)viewDidLoad {
UINib *cellNib = [UINib nibWithNibName:#"NibCell" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:#"cvCell"];
self.collectionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"shelves.png"]];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
NSLog(#"1");
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [_allEntries count];
NSLog(#"2");
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
static NSString *cellIdentifier = #"cvCell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
UIImageView *titleLabel = (UIImageView *)[cell viewWithTag:100];
UILabel *titleLabel2 = (UILabel *)[cell viewWithTag:200];
NSString *thearticleImage = entry.articleImage;
[titleLabel setImageWithURL:[NSURL URLWithString:entry.articleImage] placeholderImage:[UIImage imageNamed:#"icon#2x.png"]];
// [titleLabel2 setText:entry.articleTitle];
return cell;
}
I am using an XIB to create the collectionView cell.
When there are only 2 issues, it looks fine, but as more and more issues get added and you begin to have to scroll, they get quite off:
Your calculation of your UICollectionViewCell with paddings between cells could be kind a wrong. You can either play with size of images or set the size of cell programmatically:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(192.f, 192.f);
}
You can also play with paddings in Interface Builder.
You can also do this programmatically:
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(100, 200)];
[flowLayout setMinimumInteritemSpacing:10];
[flowLayout setMinimumLineSpacing:10];

iOS : Collection view doesn't show

I have a collection view in my app, I want it to contain a custom cells. I've created a custom cell view xib file. Then I use it in my data source method :
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
OtherCustomersCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:OTHER_CUSTOMERS_CELL_IDENTIFIER forIndexPath:indexPath];
if(cell == nil){
NSArray *nsObjects = [[NSBundle mainBundle] loadNibNamed:#"OtherCustomersCell" owner:nil options:nil];
for(id obj in nsObjects)
if([obj isKindOfClass:[OtherCustomersCell class]])
cell = (OtherCustomersCell*) obj;
}
[cell.name setText:#"AAAA BBBBB"];
return cell;
}
But when I run the app, there is just a black rectangular where the collection view should be (at the bottom under the table view):
What am I doing wrong?
Thank you in advance.
Collection views work differently than table views in that you don't have to create a cell if one can't be dequeued.
Instead, you have to register the nib for the cell first:
- (void)viewDidLoad
{
...
UINib *cellNib = [UINib nibWithNibName:#"OtherCustomersCell" bundle:nil];
[collectionView registerNib:cellNib forCellWithReuseIdentifier:OTHER_CUSTOMERS_CELL_IDENTIFIER];
}
You can then dequeue the cell and it will be created automatically for you if necessary:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
OtherCustomersCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:OTHER_CUSTOMERS_CELL_IDENTIFIER forIndexPath:indexPath]; // cell won't be nil, it's created for you if necessary!
[cell.name setText:#"AAAA BBBBB"];
return cell;
}
You have to register UICollectionView instance in the following way in viewdidload then you can use this.
[self.photoListView registerNib:[UINib nibWithNibName:#"UIcollectionViewCell" bundle:nil] forCellWithReuseIdentifier:#"Identifier"];

Resources