In my app, I'm displaying a calendar, and I am using a collection view to show the labels for the days of the week. For some reason, all of the labels still show the default "Label" when I run the app. I looked at a similar question, but it didn't help. Any suggestions? Here is the code for the UICollectionViewCell:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CalendarTitleCellIdentifier forIndexPath:indexPath];
UILabel *label = (UILabel *)[cell viewWithTag:1];
label = [[UILabel alloc] init];
CGSize size = [self collectionView:collectionView layout:collectionView.collectionViewLayout sizeForItemAtIndexPath:indexPath];
label.frame = CGRectMake(0, 0, size.width, size.height);
NSString *title = [[NSString alloc] initWithFormat:#"%#",self.weektitles[indexPath.row]];
label.text = title;
NSLog(#"%#", label.text);
return cell;
}
FYI, I have an actual label in the storyboard (in the collection view cell).
Because you create a new UILabel point label.so you revise label.text not cell label.text.
Delete
label = [[UILabel alloc] init]
Create a UICollectionViewCell name it for example DayCell with property
#property (weak, nonatomic) IBOutlet UILabel *lblName;
In storyboard change the cell Class from UICollectionViewCell to DayCell also set Identifier as "daycell" now bind the label lblName to the label in the storyboard
And set the cellForItemAtIndexPath method
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"daycell";
DayCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
[cell.lblName setText:#"XYZ"];
return cell;
}
Make sure you have properly set the delegate and datasource for the collection view.
Both lines of code return a UILabel:
UILabel *label = (UILabel *)[cell viewWithTag:1];
label = [[UILabel alloc] init];
The latter recreated a label and it doesn't belong to UICollectionViewCell anymore
Related
Implementing the title under the image in UICollectionView. I am new in this ios application development. I want to implement the title under the image view in collectionView. Does anybody know the answer?
I have initialized the label programmatically and add it in the collectionview but it did not show the title
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
static NSString *identifier = #"Cell";
//MARK:-To set the variables
self.imageArray=[NSArray arrayWithObjects:#"1.png",#"2.png",#"3.jpeg",#"4.jpeg",#"1.png",#"2.png",#"3.jpeg",#"4.jpeg"];
self.imageText=#[#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8"];
// for image
UIImageView *recipeImageView = [[UIImageView alloc] init];
recipeImageView.frame = self.collectionView.bounds;
[self.collectionView addSubview:recipeImageView];
recipeImageView.tag = 100;
// for the label
UILabel *title=[[UILabel alloc]init];
title.tag = 200;
[self.collectionView addSubview:title];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:identifier];
}
//MARK:-To set the number of sections in UICOLLECTIONVIEW
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.imageArray.count;
}
//MARK:-To set the content to the UICollectionView
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
static NSString *identifier = #"Cell";
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
//MARK:-To set the image dynamically to the UICollectionViewCell
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[self.imageArray objectAtIndex:indexPath.row]];
[self.view addSubview:recipeImageView];
cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:[self.imageArray objectAtIndex:indexPath.row]]];
//MARK:- To set the label dynamically to the UICollectionViewCell
UILabel *imageTitle=(UILabel *)[cell viewWithTag:200];
[imageTitle setText:self.imageText];
return cell;
}
#end
I've got a UICollectionViewController which displays blocks of telephone numbers (see image). When the view loads they all appear fine however when i either begin scrolling, changing rotation, or execute a search function which alters the (mutable) array in which the data is sourced, i see these malformed labels. I did think it might be the iOS simulator however from looking at it, it appears to be an issue with the positioning of UICollectionViewCells.
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor grayColor];
cell.layer.cornerRadius = 5;
[cell setClipsToBounds: YES];
CGRect cellBound = CGRectMake(25, 12.5, 150, 12.5); // x, y, w, h
UILabel *title = [[UILabel alloc] initWithFrame:cellBound];
NSString *number = [[searchNumbers objectAtIndex:indexPath.row] valueForKey:#"number"];
number = [number stringByReplacingOccurrencesOfString:#"+44" withString: #"0"];
title.text = number;
[cell addSubview:title];
return cell;
}
It should be noted that i am using UICollectionViewFlowLayout
As #Woodstock mentioned, this is due to "over-adding" UILabel objects to your cell.
Rather than his solution, which still adds the UILabel to the cell in -collectionView:cellForRowAtIndexPath:, the better MVC solution is this:
// A UICollectionViewCell subclass
// Make sure to pick the correct "init" function for your use case
- (instancetype)init... {
self = [super init...];
if (self != nil) {
[self setupCell];
}
return self;
}
- (void)setupCell {
self.backgroundColor = [UIColor grayColor];
self.clipsToBounds = YES;
self.layer.cornerRadius = 5;
CGRect cellBound = CGRectMake(25, 12.5, 150, 12.5); // x, y, w, h
// Assumes you've set up a UILabel property
self.titleLabel = [[UILabel alloc] initWithFrame:cellBound];
[cell addSubview:self.titleLabel];
}
- (void)configureWithNumber:(NSString *)number {
number = [number stringByReplacingOccurrencesOfString:#"+44" withString: #"0"];
self.titleLabel.text = number;
}
// In your UICollectionViewDataSource/Delegate implementation
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = #"cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
NSString *number = [[searchNumbers objectAtIndex:indexPath.row] valueForKey:#"number"];
[cell configureWithNumber:number];
return cell;
}
Basically, you want to set up and add views only when setting up the cell initially. After that, you should pass in a data value/object and configure the cell. If you have cells that need different controls (2 labels vs. 1, etc.), then make multiple subclasses. This way, you encapsulate your classes for cleaner code and better reuse.
I believe this is happening because you are adding more and more UILabel subviews to your cell (over and over again as cellForItemAtIndexPath is called). You need to add a check and only add a label subview if the cell doesn't already have one. The dequeued cells already have the label subview if they're being reused, if this label already exists you simply need to set it's text from your datasource.
Pseudocode:
for subview in subviews {
if subview.isKindOfClass(UILabel) {
// assign the new text label.
}
else
{
// create and add the UILabel subView.
}
}
This is an easy mistake to make as dequeueReusableCellWithReuseIdentifier can either give you a previously used cell OR as you've seen give you a fresh one initially. Which is why the app works correctly when you start, but gets messy as you scroll.
I'm trying to fill a collectionview with data from an array which I've checked it has the data, but at runtime it's filling just the cell at index 0.
The collectionview is a list of friends in pages of 9 items showing photo and name. The number of items at section is working properly, I mean, if the array has 3 objects, the collectionview displays three cells but just the first one with the photo and name of the object, concretly the last one in the array, not the first one. And the other cells show the prototype cell.
I guess I'm dealing wrong with the indexpath of the collection view, but I have another one in my storyboard and works properly. This other one has only one cell per page, could be something related to this?
I paste my collectionview methods:
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self.miListaAmigos count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"friendCell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
UILabel *nameLabel = (UILabel *)[self.view viewWithTag:102];
nameLabel.font = [UIFont fontWithName:#"Ubuntu" size:12.0];
nameLabel.text = [[self.miListaAmigos objectAtIndex:indexPath.row] valueForKey:#"usr_username"];
return cell;
}
you not directly reuse UILabel from self.view, UILabel *nameLabel = (UILabel *)[self.view viewWithTag:102];
try this below code for solve your problem:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"friendCell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell == nil)
{
//Create UILabel here
UILabel *nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(5, 5, 310, 20)];
nameLabel.tag=100;
[cell addSubview:nameLabel];
nameLabel.font = [UIFont fontWithName:#"Ubuntu" size:12.0];
}
// Access label and reuse
UILabel *nameLabel = (UILabel *)[cell viewWithTag:100];
nameLabel.text = [[self.miListaAmigos objectAtIndex:indexPath.row] valueForKey:#"usr_username"];
return cell;
}
Check your collectionView alloc and required deleagte:
UICollectionViewDelegatećUICollectionViewDataSourceć UICollectionViewDelegateFlowLayout.
e.g.
UICollectionViewFlowLayout *collectionLayout = [[UICollectionViewFlowLayout alloc] init];
[collectionLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:collectionLayout];
[collectionView registerClass:[CNCollectionViewCell class] forCellWithReuseIdentifier:cellIndetify];
collectionView.delegate = self;
collectionView.dataSource = self;
[self.view addSubview:collectionView];
Your label is in self.view not in the cell... You really should make a subclass of UICollectionViewCell
File -> New -> File -> Cocoa Touch Class
Class: MYFriendCollectionViewCell
Subclass of: UICollectionViewCell
Also Create XIB: Tick
Language: Objective C
Look at MYFriendCollectionViewCell.xib and make your label inside here... connect the labels to an IBOutlet... don't use tags...
#property (weak, nonatomic) IBOutlet UILabel *nameLabel;
https://www.youtube.com/watch?v=GusRijNLUGg <- connecting IBOutlets
Add the line below in your ViewController
[self.collectionView registerNib:[UINib nibWithNibName:#"MYFriendCollectionViewCell" bundle:nil] forCellWithReuseIdentifier: CellIdentifier];
Change your cellForItem to:
static NSString *CellIdentifier = #"friendCell";
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath{
MYFriendCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: CellIdentifier forIndexPath: indexPath];
cell.nameLabel.font = nameLabel.font = [UIFont fontWithName:#"Ubuntu" size:12.0]; // This ideally would be in MYFriendCollectionViewCell.m
cell.nameLabel.text = [[self.miListaAmigos objectAtIndex:indexPath.row] valueForKey:#"usr_username"];
return cell;
}
I have a UICollectionView that shows images, similar to cover art, or iBooks. I would like for it to show the title of the audio clip underneath the UIImageView. I have in my MainWindow.xib a View Controller with a UICollectionView inside it. I also built a NibCell.xib for the cell itself. In the Cell, I have a UIImageView that fills up all but the bottom 30 px of the cell. In this area I add a UILabel. I give the UIImageView a tag of 100 and UILabel a tag of 200, and in my code I put:
-(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;
}
However, no matter how the cell is set up in NibCell.xib, it makes the UIImageView fill the entire cell, and adds the UILabel on top of it. Any suggestions?
You need to add your UILabel to cell's contentview
UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, cell.bounds.size.width, 40)];
title.tag = 200;
[cell.contentView addSubview:title];
I have a UILabel withing a UICollectionViewCell object.
#interface TideDataTableCell : UICollectionViewCell
#property (strong, nonatomic) NSString* dayString;
#property (weak, nonatomic) IBOutlet UILabel *dayLabel;
#end
The label is synthesized in the m file for the cell object. When I try to assign the text property though, the label object is always null. Even creating a new label and assigning it to the cell dayLabel does not work! The code below is just the straight assignment to the label as nothing seems to work...
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Main Tide Data Table Cell";
TideDataTableCell* tideDayDataCell = [self.tideDataTable dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
tidalDate* tideDate = self.tidalDates[indexPath.row];
self.tideDataTable.backgroundColor = [UIColor lightGrayColor];
tideDayDataCell.backgroundColor = [UIColor whiteColor];
tideDayDataCell.dayLabel.textColor = [UIColor blackColor];
tideDayDataCell.dayLabel.text = tideDate.dateString;
return tideDayDataCell;
}
Why does this not work?! I have checked that the label in the UICollectionViewCell is connected to the dayLabel in the cells h file (above)
You need to register your cell on the viewDidLoad of TideDataTableCell like this:
UINib *cellNibName = [UINib nibWithNibName:#"cellNibName" bundle:nil];
[self.collectionView registerNib:cellNibName forCellWithReuseIdentifier:#"cellIdentifier"];
Then, at cellForItemAtIndexPath you have to get the cell and use it:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"cellIdentifier";
TideDataTableCell* tideDayDataCell = [self.tideDataTable dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
tidalDate* tideDate = self.tidalDates[indexPath.row];
self.tideDataTable.backgroundColor = [UIColor lightGrayColor];
tideDayDataCell.backgroundColor = [UIColor whiteColor];
tideDayDataCell.dayLabel.textColor = [UIColor blackColor];
tideDayDataCell.dayLabel.text = tideDate.dateString;
return tideDayDataCell;
}
Don't forget to set the reuse identifier in the xib file.