I have added a UILabel on the UICollectionViewCell through the xib.
I have made the necessary objects in the class and connected it appropriately.
In the cellForItemAtIndexPath method i get access to the UILabel declared but the label is always nill.
I can add background color to the Cell programmatically but not via the Xib. I can also add the label programmatically and work with it.
But i cant do anything with the label via the xib.
I have registered the Cell class and the nib.
Would be great if someone could help me out with this issue
the code snippet inside cellforitem-
static NSString *cellIdentifier = #"Cell";
VideoCellCollection *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
cell.backgroundColor=[UIColor whiteColor];
[cell clearData];
VideoInfo *video=[dataArray objectAtIndex:indexPath.item];
cell.lblName.text=video.strFileName;
return cell
Code inside viewdidload
UINib *cellNib = [UINib nibWithNibName:#"VideoCellCollection" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:#"Cell"];
[self.collectionView registerClass:[VideoCellCollection class] forCellWithReuseIdentifier:#"Cell"];
Thanks a lot in advance
Try to remove [self.collectionView registerClass:[VideoCellCollection class] forCellWithReuseIdentifier:#"Cell"];
Related
So I have a UICollectionView and a custom cell and it all shows up and works great. I register the class in viewDidLoad:
myCollectionView = [[UICollectionView alloc] initWithFrame:collectionViewFrame collectionViewLayout:layout];
[myCollectionView setDataSource:self];
[myCollectionView setDelegate:self];
[myCollectionView setBackgroundColor:[UIColor myColor]];
[myCollectionView registerClass:[SBCustomCell class] forCellWithReuseIdentifier:#"Cell"];
In my cellForItemAtIndexPath method I dequeue the cell and set its properties and return it:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"Cell";
SBCustomCell *cell= [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
[cell setName: "My Name"];
cell.image = [UIImage imageNamed:#"lol.png"];
cell.score = 100.0;
cell.backgroundColor=[UIColor whiteColor];
return cell;
}
This all works fine and it shows up in the UI. My issue is when I set a gesture recognizer on the collectionView, when I long press a certain cell, I want to be able to access its properties. I am trying to do so as such:
-(void)handleLongPress:(UILongPressGestureRecognizer *)longPressRecognizer {
CGPoint locationPoint = [longPressRecognizer locationInView:myCollectionView];
if (longPressRecognizer.state == UIGestureRecognizerStateBegan) {
NSIndexPath *indexPathOfMovingCell = [myCollectionView indexPathForItemAtPoint:locationPoint];
SBCustomCell *cell= [myCollectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPathOfMovingCell];
NSLog(#"%#",cell.name);
When I try to access any of my custom cell's properties, it is (null) in console. Why is that? What am I doing wrong?
You need to use:
SBCustomCell* cell = (SBCustomCell*)[myCollectionView cellForItemAtIndexPath:indexPathOfMovingCell];
instead of:
SBCustomCell* cell = [myCollectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPathOfMovingCell];
Also, you are using SBSingleCandidateUnitView as the cell type instead of your SBCustomCell.
It was because I wasn't setting my properties correctly. In my header file I need to be setting a property, aka #property ... UIImageView myImageView;
And in my CustomCell.m file I should be not overriding those setters and getters. Instead just alloc and initing them and adding them to the view.
And back in my ViewController.m I should have added the properties as such:
customcell.myImageView.image = [UIImage imageNamed:#"cartman.png"];
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...
I have two types of UICollectionViewCell : one is gallery cell and another is recipe cell.
There is a toggle button.
First time the view loads, gallery cell is used to display in collection view.
But on clicking the toggle button, I tried to load another cell:(recipe cell) but it crashed saying there is no property(actually this is the property of recipe cell) in gallery cell.
Is there a way to load same collection view with another cell??
Thanks in advance.
EDIT:
if (isGalleryOnCollectionView) {
ProfileFollowerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cell" forIndexPath:indexPath];
NSString *followerName;
if ([[arr_followerNames objectAtIndex:indexPath.row] isEqualToString:#""]) {
followerName=[arr_followersEmail objectAtIndex:indexPath.row];
}
else{
followerName=[arr_followerNames objectAtIndex:indexPath.row];
}
NSLog(#"\n\n\n follower name is :: %#\n\n",followerName);
cell.lbl_followerName.text = followerName;
}else{
GalleryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cell" forIndexPath:indexPath];
cell.lbl_recipeName.text = [[[dict_profileData valueForKey:#"recipes"] valueForKey:#"name"] objectAtIndex:indexPath.row];
}
SOLUTION:
[self.profleCollectionView registerClass:[ProfileFollowerCell class] forCellWithReuseIdentifier:#"followercell"];
After registering from the nib before dequeuing reusable identifier it works.
Here is a good tutorial on creating custom UICollectionViewCell with xibs.
What your missing is having this in your -(void)viewDidLoad
UINib *cellNib = [UINib nibWithNibName:#"ProfileFollowerCellNib" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:#"TheNibCellIdentifier"];
You also need to make sure to use the respective ReuseIdentifier when loading the cell:
ProfileFollowerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"TheNibCellIdentifier" forIndexPath:indexPath];
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 !
OK in my story board I have made a UICollectionView with 3 cells. One of the cells I made a custom class for that obviously extends the UICollectionViewCell:
And I registered the class in my ViewDiDApear:
[self.collectionView registerClass:[DeviceImageCell class] forCellWithReuseIdentifier:#"Cell1"];
Also I have added an imageView to that specific cell and an outlet for that imageView in my custom class. Problem occurs when I make my custom cell it forgets everything that was set in my storyboard, aka the background color, where my image view is and so on. Because of this my imageView returns nil all the time.
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == 0)
{
static NSString *identifier = #"Cell1";
DeviceImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImage *image = [UIImage imageNamed:#"dysart-woods-full.jpg"];
cell.imageview.image = image;
cell.backgroundColor = [UIColor blueColor];
//cell.imageview.image = image;
return cell;
}
else if(indexPath.row == 1)
{
static NSString *identifier = #"Cell2";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
return cell;
}
else
{
static NSString *identifier = #"Cell3";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *imageView = (UIImageView *)[cell viewWithTag:1];
imageView.image = [UIImage imageNamed:#"dysart-woods-full.jpg"];
return cell;
}
}
You have probably long solved this issue.
However for those finding this and having the same issue
it is the registerClass call in your viewDidAppear
[self.collectionView registerClass:[DeviceImageCell class] forCellWithReuseIdentifier:#"Cell1"];
You are already registering the class in Interface Builder, so the call to registerClass:forCellWithReuseIdentifier: is replacing the entry created by IB.
Removing this line will fix the issue.
You should register the custom cell's nib if you are using one like this-
So it will be:
[self.collectionView registerNib:[UINib nibWithNibName:#"CustomCell" bundle:nil] forCellWithReuseIdentifier:#"CustomCellIdentifier"];
instead of:
[self.collectionView registerClass:[CustomCell class] forCellWithReuseIdentifier:#"CustomCellIdentifier"];
Well, this is odd. I know a solution, but I don't quite understand why it makes a difference. I had the same exact setup and the same problem. The IBOutlet was getting set to nil for the UIImageView, but not the UILabels.
Change your declaration for the UIImageView to be a strong property instead of an instance variable.
#property (strong) IBOutlet UIImageView* imageView;
This solved the issue for me. Clearly there is something with the way that UIImageView is connected / instantiated from the storyboard. As an added 'weird' factor, I use the same setup of ivars with a UITableViewCell and have no issue, only in the UICollectionViewCell
I had the similar problem and it's been solved by adding the following code in the DeviceImageCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_imageview = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
[self.contentView addSubview:_imageview];
}
return self;
}
Unfortunately I haven't managed to get _imageview initialised without this code addition.
Also check this one,
So now when you declare Custom CollectionView Cell in its Storyboard you need to specify the Attribute named "Identifier" under Collection Reusable View to some String.
Now this may sound weired but ,
Check if name of Identifier is not same as Collection View Cell's class name.
Class/File name of Cell and Cell Identifier must be Different.
That really worked for me , hence posting.