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.
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
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
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 implemented a UICollectionView inside a UIViewController. The header cell is created in a separate.xib file and it is implemented in a separate .h and .m files with the IBOutlets and IBActions. The rest of the cells are implemented in the same UIVIewController (The reason is because I added this Parallax effect).
I would like to modify the info of the IBoutlets (labels and buttons) that are in the header cell (CSCellUser.h) from the viewcontroller (RankingViewController) which containts the collectionview, how can I do this?
Cell: CSCellUser.h
#interface CSCellUser : UICollectionViewCell
#property IBOutlet UILabel *myScoreValueLabel;
#property IBOutlet UILabel *myRankingValueLabel;
-(IBAction) sendButtonTouchHandler:(id) sender;
#end
UIViewController: RankingViewController.h
#interface RankingViewController : CommonViewController <UICollectionViewDelegate, UICollectionViewDataSource> {
}
#property IBOutlet UICollectionView *collectionView1;
#end
UIViewController:RankingViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
//Parallax Effect, UICollectionView
// Locate the layout
CSStickyHeaderFlowLayout *layout = (id)self.collectionView1.collectionViewLayout;
if ([layout isKindOfClass:[CSStickyHeaderFlowLayout class]]) {
layout.parallaxHeaderReferenceSize = CGSizeMake(320, 220);
layout.parallaxHeaderAlwaysOnTop = YES;
}
// Locate the nib and register it to your collection view
UINib *headerNib = [UINib nibWithNibName:#"CSHeaderRanking" bundle:nil];
[self.collectionView1 registerNib:headerNib
forSupplementaryViewOfKind:CSStickyHeaderParallaxHeader
withReuseIdentifier:#"TopViewCell"];
//get the position of the user and the ranking (this should update the IBOutlets in the CSCellUser.h)
[self getUserRanking];
//get the ranking of users (this updates each cell of the ranking in cellForItemAtIndexPath)
[self getRanking];
}
- (NSInteger) numberOfSectionsInCollectionView:
(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger) collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return [ranking count];
}
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: #"UsersCell" forIndexPath:indexPath];
if ([ranking count] > 0)
{
UserRanking *user = [ranking objectAtIndex:indexPath.row]; //Fill the cell
UILabel *usernameLabel = (UILabel *)[cell viewWithTag:101];
usernameLabel.text = user.username;
UILabel *scoreLabel = (UILabel *)[cell viewWithTag:102];
scoreLabel.text = [NSString stringWithFormat:#"%d", user.score];
UILabel *gamesLabel = (UILabel *)[cell viewWithTag:103];
gamesLabel.text =[NSString stringWithFormat:#"%d", user.tries];
}
return cell;
}
//Header
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableview = nil;
if ([kind isEqualToString:CSStickyHeaderParallaxHeader]) {
reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:#"TopViewCell" forIndexPath:indexPath];
}
return reusableview;
}
EDIT:
This is how I am trying to change one of the labels from cellForRowAtIndexPath but it does not change.
CSHeaderRanking *topcell = [collectionView dequeueReusableCellWithReuseIdentifier: #"TopCell" forIndexPath:indexPath];
topcell.myScoreValueLabel.text = #"32";
If TopCell is mapped in its xib, all you have to do is refer to its properties. If you want to customize, you'll need to over-ride the CCSticky... and refer to it in your xib and this class.
I have creates a UICollectionView which I currently have showing white cells. I have been following this tutorial to figure out how to use the delegate methods etc.
I have made it up to the point where you create the Creating custom UICollectionViewCells however in this example the tutorial he tells you to open your story board etc. I don't have a story board. I have tried to follow the instructions to create the view but with my own .xib file and I just cannot get anything to work.
How can I check out where my delegate methods are at this point?
// this is how I load the collection view
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
photoCollectionView =[[UICollectionView alloc] initWithFrame:CGRectMake(10.0, 40.0, 480.0, 713.0) collectionViewLayout:layout];
photoCollectionView.dataSource = self;
photoCollectionView.delegate = self;
[self.view addSubview:photoCollectionView];
[photoCollectionView.layer setBorderColor: [[UIColor lightGrayColor] CGColor]];
[photoCollectionView.layer setBorderWidth: 1.0];
[self.photoCollectionView reloadData];
// this is what my delegate methods look like
#pragma mark - CollectionView Delegates
#pragma mark -- UICollectionView Datasource
// 1
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
return [imageArray count];
}
// 2
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
return 1;
}
// 3
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:#"PhotoCell" forIndexPath:indexPath];
// I think this is where I want to set my image for the cell
NSDictionary *currentPhotoDict = [imageArray objectAtIndex:indexPath.row];
UIImage *imageForCollection = [UIImage imageWithData:[currentPhotoDict objectForKey:#"DImage"]];
// but I have no idea where to pass the imag....
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
#pragma mark -- UICollectionView Delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
// TODO: Select Item
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
// TODO: Deselect item
}
#pragma mark –- UICollectionViewDelegate FlowLayout
// 1
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGSize retval = CGSizeMake(210, 157+20); // add 20 for labels under neath.. might need to adjust this.
return retval;
}
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(50, 20, 50, 20);
}Subview:photoCollectionView];
[photoCollectionView.layer setBorderColor: [[UIColor lightGrayColor] CGColor]];
[photoCollectionView.layer setBorderWidth: 1.0];
[self.photoCollectionView reloadData];
As you can see in collectionView:cellForItemAtIndexPath I have my image ready
NSDictionary *currentPhotoDict = [imageArray objectAtIndex:indexPath.row];
UIImage *imageForCollection = [UIImage imageWithData:[currentPhotoDict objectForKey:#"DImage"]];
I just need to know how to load it into the collectionviewcell.
Please try to use like this....
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:#"PhotoCell" forIndexPath:indexPath];
// I think this is where I want to set my image for the cell
NSDictionary *currentPhotoDict = [imageArray objectAtIndex:indexPath.row];
UIImage *imageForCollection = [UIImage imageWithData:[currentPhotoDict objectForKey:#"DImage"]];
[cell setBackgroundColor:[UIColor colorWithPatternImage:imageForCollection];// Add your image here........
// but I have no idea where to pass the imag....
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
i hope it will help you...
I have UICollectionViewCell with name PhotoCell. and have label and imageview as properties.
This is how u can set Image to cell. May this is your requirement or not. I dont know. Try this
You need to set identifier at Xib of your UICollectionViewCell as MY_CELL or some other name.Then
.m
static NSString *cellidentity=#"MY_CELL";
At ViewDidLoad method
UINib *nib;
nib = [UINib nibWithNibName:#"PhotoCell" bundle:nil];
[self.collectionView registerNib:nib forCellWithReuseIdentifier:cellidentity];
My UICollectionViewCell .h is
#interface PhotoCell : UICollectionViewCell
#property (retain, nonatomic) IBOutlet UILabel* label;
#property (retain, nonatomic) IBOutlet UILabel* timelineLabel;
#property (retain, nonatomic) IBOutlet UIImageView* yourImageView;
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
// PhotoCell *cell = [cv dequeueReusableCellWithReuseIdentifier:#"PhotoCell" //forIndexPath:indexPath];
PhotoCell *cell = [cv dequeueReusableCellWithReuseIdentifier:cellidentity forIndexPath:indexPath];
NSDictionary *currentPhotoDict = [imageArray objectAtIndex:indexPath.row];
UIImage *imageForCollection = [UIImage imageWithData:[currentPhotoDict objectForKey:#"DImage"]];
// but I have no idea where to pass the imag....
cell.label.text = [NSString stringWithFormat:#"%d",indexPath.item];
cell.yourImageView.image=imageForCollection;
return cell;
}
Hope it helps You....