Add UILabel Under UICollectionView Cell - ios

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];

Related

Label in UICollectionViewCell not updating

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

collectionview just filling one cell

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;
}

How to give text to UILabel dynamically from NSArray [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to create collection view in iOS application where an image and label will be displayed. I am able to display the images in Collection view, but am facing problem with UILabel. I have:
NSArray *categoryArray;
categoryArray = [NSArray arrayWithObjects:#"abc",#"xyz",#"qwe",#"asdf",#"fgh",#"hjk",#"lkj",#"ghj",#"sdf",nil];.
Now I want this text to be assigned dynamically to the UILabel which is placed in Collection view.
Here's what I have tried.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[recipeImages objectAtIndex:indexPath.row]];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"photo-frame.png"]];
//UILabel *txtCatName = (UILabel *)[cell viewWithTag:100];
// txtCatName.text = #"Testing";
return cell;
}
Put this after the line
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"photo-frame.png"]];
/*This is to avoid overlapp due to cell reuse.This will remove all labels in the cell. If only a particular label is to be removed put a tag range to it like label.tag = 100+indexPath.row and change the if accordingly to match this tag range*/
for (UIView *view in cell.subviews) {
if ([view isKindOfClass:[UILabel class]]) {
[view removeFromSuperview];
}
}
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];//Set the frame as per your requirement.
[label setText:[NSString stringWithFormat:#"%#",[categoryArray objectAtIndex:indexPath.row]]];
[cell addSubview:label];
return cell;
Also if you are using custom cell, add a UILabel to the cell and create an IBOulet property and then update the text in this method. No need to worry about overlapping then. It would be better to add through xib since looping through all the subviews will cause performance problem if the number of subviews increase. Hope this helps.
Put the Label in the UICollectionViewCell and then in the same way as you have added the image, add the label text dynamically. Edit your function code to something like this.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[recipeImages objectAtIndex:indexPath.row]];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"photo-frame.png"]];
cell.txtLabel.text = [categoryArray objectAtIndex:indexPath.row];
//UILabel *txtCatName = (UILabel *)[cell viewWithTag:100];
// txtCatName.text = #"Testing";
return cell;
}
Change the Label Tag and try this. (set Label Tag:99)
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[recipeImages objectAtIndex:indexPath.row]];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"photo-frame.png"]];
UILabel *txtCatName = (UILabel *)[cell viewWithTag:99];
txtCatName.text = [categoryArray objectAtIndex: indexPath.row] ";
return cell;
}

Much memory usage in UITableView

I have an UITableViewController and I customize its UITableViewCells. Each UITableViewCell contains two UIImageViews and two UILabels. Although when I get into the UITableView the memory usage raises to 100 MB and i do not know the reason. Here is the code for the cellForRowAtIndexPath method that handles the cell customization. Can anyone help me with this issue?
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = #"Cell";
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:simpleTableIdentifier];
}
UILabel *mnhmeioLabel = (UILabel *)[cell viewWithTag:102];
mnhmeioLabel.frame = CGRectMake(mnhmeioLabel.frame.origin.x,
mnhmeioLabel.frame.origin.y,
191,
21);
Group *tmpGroup = [mnhmeiaTotal objectAtIndex:indexPath.row];
NSLog(#"%# ",tmpGroup.title);
NSLog(#"%f %f", mnhmeioLabel.bounds.size.width,
mnhmeioLabel.bounds.size.height);
mnhmeioLabel.text = tmpGroup.title;
mnhmeioLabel.font = [UIFont fontWithName:#"Open Sans" size:10];
mnhmeioLabel.numberOfLines=0;
mnhmeioLabel.lineBreakMode=NSLineBreakByWordWrapping;
[mnhmeioLabel sizeToFit];
if( tmpGroup.imagesOfArticle.count > 0 ){
UIImageView *mnhmeioImageView = (UIImageView *)[cell viewWithTag:103];
mnhmeioImageView.image = [tmpGroup.imagesOfArticle objectAtIndex:0];
} else {
UIImageView *mnhmeioImageView = (UIImageView *)[cell viewWithTag:103];
mnhmeioImageView.image = [UIImage imageNamed:#""];
}
UIImageView *markerImageView=(UIImageView *)[cell viewWithTag:100];
markerImageView.image=[self selectMarker:tmpGroup];
UILabel *mnhmeioDescription = (UILabel *)[cell viewWithTag:101];
mnhmeioDescription.text=tmpGroup.constructedContent;
mnhmeioDescription.font = [UIFont fontWithName:#"Open Sans" size:6];
}
Probably, the images are big.
Or, perhaps you are not reusing the cells properly, if you are using iOS7 and Storyboards, please follow this tutorial (Prototype cells and Using subclass for a cell sections):
http://www.raywenderlich.com/50308/storyboards-tutorial-in-ios-7-part-1
Thank you all for your help. I had to decrease the size of each image as much as i could. This fixed my problem.

UILabel height inside UITableViewCell

I'm trying to create a tableview where the height of the cells are dynamic.
So far I manage to set the height of the cells depending on the custom UILabel I've added inside.
With the regular cell.textLabel it works fine, but when I use my own label something goes wrong. I only see half the label, but when I scroll up and down, sometimes the label extends and shows all the text... You can see where the label should end in the image.
Image
This is the text inside the cellForRowAtIndexPath:
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell.
Car *carForCell = [cars objectAtIndex:indexPath.row];
UILabel *nameLabel = [[UILabel alloc] init];
nameLabel = (UILabel *)[cell viewWithTag:100];
nameLabel.numberOfLines = 0;
nameLabel.text = carForCell.directions;
[nameLabel sizeToFit];
[nameLabel setBackgroundColor:[UIColor greenColor]];
return cell;
Unless you have typos in the code you posted, you don't seem to be adding the label to the cell at all. You also seem to be creating a new label every time, and then replacing the contents of your nameLabel pointer with the cell's view (which will always be nil).
Try doing something like this first and then see how it looks:
static NSString *CellIdentifier = #"Cell";
UILabel *nameLabel;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
nameLabel = [[UILabel alloc] init];
nameLabel.tag = 100;
nameLabel.numberOfLines = 0;
[nameLabel setBackgroundColor:[UIColor greenColor]];
[cell.contentView addSubview:nameLabel];
}
else {
nameLabel = (UILabel *)[cell viewWithTag:100];
}
// Configure the cell.
Car *carForCell = [cars objectAtIndex:indexPath.row];
nameLabel.text = carForCell.directions;
[nameLabel sizeToFit];
return cell;
You will also need to tell the tableView what size each cell needs to be using the tableView:heightForRowAtIndexPath: delegate method. That will mean getting the relevant Car object again and calculating the height using sizeWithFont:sizeWithFont:forWidth:lineBreakMode:
How are you setting the height of the cell? It should be done in - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
You should calculate and return the height of the UITableViewCell in the following method:
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
Here you should do your initial calculation of how high your cell should be.
For example:
CGSize textSize = [myString sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(320, 9999)];
return textSize.height;

Resources