I am implementing a header for my UICollectionView right now, and I am struggling right now since the header is called only once on the first cell.
This is what I've done - I declared UICollectionViewFlowLayout to programmatically make a UICollectionView - in this case, pickView. And then I added two xib files - one for cell, and the other one for header. I have registered those files into collection view like this:
UICollectionViewFlowLayout *layout2 = [[UICollectionViewFlowLayout alloc] init];
self.pickView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, pointView_y, pointView_width, pointView_length) collectionViewLayout:layout2];
self.pickView.backgroundColor = [UIColor whiteColor];
[self.pickView registerNib:[UINib nibWithNibName:#"TodayCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:#"TodayCollectionViewCell"];
[self.pickView registerNib:[UINib nibWithNibName:#"PickCollectionReusableView" bundle:[NSBundle mainBundle]] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"PickCollectionReusableView"];
//layout2.headerReferenceSize = CGSizeMake(_screenWidth, 50);
[self.pickView setDataSource:self];
[self.pickView setDelegate:self];
[self.view addSubview:self.pickView];
And then I declared the size of header view like this:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
return CGSizeMake([Util screenWidth], 50);
}
And finally I called viewForSupplementaryElementOfKind to call header files like this:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
PickCollectionReusableView *reusableview = (PickCollectionReusableView*) [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"PickCollectionReusableView" forIndexPath:indexPath];
PickCellVariable *buf = (PickCellVariable*) _pickArray[indexPath.row];
NSLog(#"buf id = %#, SECTION = %lu", buf.itemID, (long)indexPath.row);
if(buf != nil) {
//reusableview.thumbPic = ;
[reusableview.brandName setTitle:[NSString stringWithFormat:#"%lu", (long)indexPath.row] forState:UIControlStateNormal];
[[reusableview brandName] setBrandName:buf.brandname];
[[reusableview brandName] addTarget:self action:#selector(brandClick:) forControlEvents:UIControlEventTouchUpInside];
[[reusableview settingButton] addTarget:self action:#selector(setting:) forControlEvents:UIControlEventTouchUpInside];
return reusableview;
}
else {
[[reusableview brandName] setBrandName:#""];
return reusableview;
}
}
I put the breakpoint and added NSLog line to make sure that viewForSupplementaryElementOfKind is only called once, in the very first cell.
I've tried to find cases like mine, but most of the issues regarding the UICollectionView header is when they registered class instead of registering xib file, or they forgot to set the size of header view. Mine is only called for once so this is not about overlap as well. My collection view is programmatically made so I do not redefine anything right now.
What could be the issue? Please, anything can help in here.
Thank you.
Ok, so my problem was that I called my data on row basis. However, in order to make header to appear on every cell, you have to call your data in section basis.
I hope no one is struggling anymore like me in terms of this issue.
Related
I have a collectionview and I want to use a xib as the header. However, the xib will not appear. First I tried adding a label to the xib, which didn't appear. Then I set the whole background color to red. It it doesn't appear. The collectionview items leave a gap for the header, but it's completely blank. Similar SO threads are this and this, but as I'm not using storyboards and my header height is greater than zero, they don't solve my issue. Here is all my relevant code:
#import "ScoreboardCollectionViewController.h"
#import "ScoreboardCollectionViewCell.h"
#import "ScoreboardReusableView.h"
#import "ScoreboardModel.h"
#import "Scoreboard.h"
...
- (void)viewDidLoad {
[super viewDidLoad];
// Register cell classes
UINib *cellNib = [UINib nibWithNibName:#"ScoreboardCollectionViewCell" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:#"cell"];
[self.collectionView registerClass:[ScoreboardReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"scoreboardHeader"];
self.collectionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"dark_fish_skin_"]];
}
...
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
return CGSizeMake(self.view.frame.size.width, 34);
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath {
ScoreboardReusableView *view = [[ScoreboardReusableView alloc] init];
view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:#"scoreboardHeader"
forIndexPath:indexPath];
return view;
}
Here is a screenshot of my xib:
OK, I solved it. The problem was that in my viewDidLoad method I had
[self.collectionView registerClass:[ScoreboardReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"scoreboardHeader"];
when since I was using a nib, it should have been:
[self.collectionView registerNib:[UINib nibWithNibName:#"ScoreboardReusableView" bundle:nil]
forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:#"scoreboardHeader"];
I was missing "Section Header" option of Collection View to enable:-
This is a duplicate of this question. I'm asking again because the accepted answer is not working and no one's providing more explanation on how the supposed correct answer works.
So here's the situation: I want to display the collection view into one single row. To do this, I applied a custom UICollectionViewFlowLayout to the collection view and set the scrolling to horizontal. Everything is working fine, except for section header disappeared.
To remedy this, I implemented this function:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
return CGSizeMake(350, 35);
}
Now the header is shown, but the problem is it is displayed to the left of the cells, instead of the usual top.
I stumbled upon the link above while searching for a solution, but like I've said, the accepted answer is not working at all and I could not find other solutions about this situation. So can anyone help me here?
we can do that by using the delegate method -
(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
and keeping the left inset to minus value of width of supplementary view and managing the top inset
Have you tried using the header with something like this?
First: set it up in viewDidLoad...
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.headerReferenceSize = CGSizeMake(self.collectionView.bounds.size.width, 30);
// add any other setup you need
[self.collectionView setCollectionViewLayout:flowLayout];
Second: add header view ...
#define LABEL_TAG 128
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:
UICollectionElementKindSectionHeader withReuseIdentifier:#"SectionHeader" forIndexPath:indexPath];
UILabel *label = (UILabel *)[headerView viewWithTag:LABEL_TAG];
if (!label) {
label = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds, 5, 5)];
label.tag = MY_HEADER_LABEL_TAG;
label.font = [UIFont boldSystemFontOfSize:12];
label.textColor = [UIColor redColor];
[headerView addSubview:label];
}
label.text = [NSString stringWithFormat:#"Section %d", indexPath.section];
return headerView;
}
Goal:
Set up two collectionViews with different styles. One grid style and other single file style. These collectionViews will be toggleable giving users shopping within the ability to view the items for sale in which ever style (grid/single file) they prefer.
I have an existing collectionView with a custom cell set up in interface builder and it's working fine. I've tried to find info on how to add a second one via interface builder but have had no luck in finding any.
What I've done:
I've created the second collectionView programatically in my viewDidLoad method. I have an instance variable named _collectionView2.
- (void)viewDidLoad
{
[super viewDidLoad];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
_collectionView2 = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
[_collectionView2 setDataSource:self];
[_collectionView2 setDelegate:self];
[_collectionView2 registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"Cell"];
[_collectionView2 setBackgroundColor:[UIColor redColor]];
[self.view addSubview:_collectionView2];
[_collectionView2 setHidden:YES];
I've modified delegate and datasource methods to make them aware about the new collectionView:
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
if (collectionView == _collectionView) {
NSArray *people = [_thisController objects];
return [people count];
} else {
return 20;
}
}
.
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
if (collectionView == _collectionView) {
static NSString *CellIdentifier = #"Cell";
VAGGarmentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: CellIdentifier forIndexPath:indexPath];
[[cell activityIndicator] startAnimating];
PFFile *userImageFile = [object valueForKey:#"image"];
[[cell imageView] setFile: userImageFile];
[[cell imageView] loadInBackground];
[[cell activityIndicator] stopAnimating];
[[cell title] setText:[object valueForKey:#"title"]];
[[cell price] setText:[NSString stringWithFormat: #"£%# GBP", [object valueForKey:#"price"]]];
return cell;
} else {
static NSString *CellIdentifier = #"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor greenColor];
return cell;
}
//_addToFavouritesButton = [cell addFavouriteButton];
[_addToFavouritesButton addTarget:_thisController action:#selector(addToFavouritesButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
.
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (collectionView == _collectionView2) {
return CGSizeMake(50, 50);
} else {
return CGSizeMake(140, 272);
}
}
Then I have a method that responds to the UISegmentControl being switched from grid style display to single file display:
- (void)displayTypeSegmentSelected
{
_selectedDisplayTypeIndex = [_displayTypeControl selectedSegmentIndex];
if (_selectedDisplayTypeIndex == 0) {
NSLog(#"Single file item view selected");
[_collectionView setHidden:YES];
[_collectionView2 setHidden:NO];
} else {
NSLog(#"Grid style view selected");
[_collectionView setHidden:NO];
[_collectionView2 setHidden:YES];
}
}
CollectionView2 is hidden initially then unhidden when the segment control is used. It looks like it's working because when I toggle a red background shows and I did set a red background when I created the collectionView but green cells aren't showing at all.
I can't seem to see where I've gone wrong. I'd like to get this working so I can replace the cell with my custom cell. The only difference between grid display and single file display is that single file display will be an enlarge cell that takes up the width of the view. All properties etc will be the exact same meaning I can use my current custom cell.
Why aren't my cells showing? I've followed a clear tutorial on creating a collectionView programmatically but still no cells are showing.
Update:
I put a log message in the if statement that checks what collectionView is present and it is only ever trigger for my _collectionView and not _collectionView2.
Help is appreciated.
Regards
Not sure this will fix your issue, but when I have 2 collection views in one controller, I use their tag's to determine which one I am dealing with in a given function, like cellForItemAtIndexPath.
I also have always used different cell identifiers for the two cells and usually different subclasses of uicollectionviewcell.
Where do you create the first collection view and set its delegate?
Reloading the table data in the method my UISegmentedControl triggers got the cells to show up.
I have a UICollectionView in one of my viewcontroller. My collection view uses a subclass of UICollectionViewLayout (custom) to layout the cells. First thing, as soon as I select Layout as Custom in dropdown on Storyboard, option to select supplementary views goes away.
I tried doing that programatically as shown below, but none of the delegate methods are getting called.
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if (kind == UICollectionElementKindSectionHeader) {
UICollectionReusableView *reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"HeaderView" forIndexPath:indexPath];
if (reusableview==nil) {
reusableview=[[UICollectionReusableView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
}
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
label.text=[NSString stringWithFormat:#"Recipe Group #%li", indexPath.section + 1];
[reusableview addSubview:label];
return reusableview;
}
return nil;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
CGSize headerSize = CGSizeMake(320, 44);
return headerSize;
}
In my viewDidLoad Method I have
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"HeaderView"];
Can anyone point me where I'm messing up?
You're passing in the incorrect view kind.
Your line registering the class:
[self.collectionView registerClass:[UICollectionReusableView class]
forSupplementaryViewOfKind:UICollectionElementKindSectionFooter
withReuseIdentifier:#"HeaderView"];
Should be:
[self.collectionView registerClass:[UICollectionReusableView class]
forSupplementaryViewOfKind: UICollectionElementKindSectionHeader
withReuseIdentifier:#"HeaderView"];
Edit: Looks like your code is all using sectionFooter. Are you trying to programmatically add a header or a footer?
Found the issue, I was not returning attributes of my header in this UICollectionLayoutView method:
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect; // return an array layout attributes instances for all the views in the given rect
Check that you gave reference size for header in UICollectionViewFlowLayout
[flowLayout setHeaderReferenceSize:CGSizeMake(320, 50)];
and for footer
[flowLayout setFooterReferenceSize:CGSizeMake(320, 50)];
Your delegate method for header reference size is wrong, you call footer method,referenceSizeForFooterInSection, as follow:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
CGSize headerSize = CGSizeMake(320, 44);
return headerSize;
}
Individually set HeaderReferenceSize will fix the header problem. But app will crash you keep the above method and return nil in viewForSupplementaryElementOfKind for Footer.
your check:
if (kind == UICollectionElementKindSectionFooter)
Your should check for: UICollectionElementKindSectionHeader
same for:
dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter
I think you should call this in your viewdidload method:
[collectionViewFlowLayout setHeaderReferenceSize:CGSizeMake(self.collectionView.frame.size.width, 50)];
I'm writing an app with a UICollectionViewController.
I've created the layout in with a storyboard. Subclassed UICollectionViewController and linked the two in IB. I've also given it a Storyboard ID of "ImageCollectionViewController".
I've also subclassed a UICollectionViewCell and linked it with my prototype cell in IB as well as given it the Identifier "ImageCell".
This is my constructor I call to create my custom UICollectionViewController:
-(id)initWithGroup:(CatalogGroup*)group
{
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPad" bundle: nil];
self = [storyboard instantiateViewControllerWithIdentifier:#"ImageCollectionViewController"];
if (self) {
[self setGroup:group];
// Register the type of cell for collection view
[self.collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:#"ImageCell"];
}
return self;
}
Now this all seems to work. All the delegate methods are being called in the collectionViewController. For example:
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self fetchedResultsController] sections][section];
return [sectionInfo numberOfObjects];
}
My cells are being initialized and passed back with:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* identifier = #"ImageCell";
ImageCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
ImageItem* item = [[self fetchedResultsController].fetchedObjects objectAtIndex:indexPath.row];
if(cell) {
// Set up label
cell.label.lineBreakMode = NSLineBreakByCharWrapping;
cell.label.numberOfLines = 0;
[cell.label setText:item.labelText];
NSData* imageData = item.imageData;
UIImage* image = [UIImage imageWithData:imageData];
UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
[cell setImageView:imageView];
}
return cell;
}
Yet when I push the view controller onto the stack. None of the cells are there?
Is there anything wrong with my stack?
The background color I set for the CollectionView in IB is showing so I believe it's being created properly. The cells are initialized. The delegate methods are being called so I don't know where to start debugging.
I'm not sure what your custom cell looks like, but generally, it should be a subview of cell.contentView:
[cell.contentView addSubview:myContentView];
So, instead of [cell setImageView:imageView], you might try
[cell.contentView addSubview:imageView];
You can test your collection view by setting the background color of the cell in collection view cell custom class. This will ensure your cells are configured properly. Now come to your problem(It is only a guess since you are not shown your cell class). If you are setting image view of the cell in nib, you should _registerNib instead of _registerClass_for the cell. Other wise you should add the image view as the sub view to the cell.contentView