UIViewController does not load its UICollectionView property - ios

I have a UITabBarController that contains 2 UIViewControllers. The 2nd UIViewController crashes when ever I try to show from the 1st UIViewController.
The 2nd UIViewController crashes because of its UICollectionView declared as a private property.
I get a EXC_BAD_ACCESS so I think that the 2nd UIViewController tries to do [self setCollectionView] but when its property self.collectionView is not init yet (still nil).
I don't understand why it behaves so - I have no problem implementing the UICollectionView the same way in the 1st UIViewController. Here is the .m file of the 2nd UIViewController :
#interface WorkoutViewController () <UICollectionViewDataSource, UICollectionViewDelegate, DAPageControlViewDelegate>
#property (strong, nonatomic) UICollectionView *collectionView;
#property (strong, nonatomic) DAPageControlView *pageControlView;
#end
#implementation WorkoutViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self createCollectionView];
[self createPageView];
// Constraints
// CollectionView
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
// PageControl
[self.pageControlView mas_makeConstraints:^(MASConstraintMaker *make) {
make.trailing.equalTo(self.view);
make.leading.equalTo(self.view);
make.height.equalTo(screenAdjustedSizeFrom(15));
make.bottom.equalTo(self.view).offset(screenAdjustedSizeFrom(-15).floatValue);
}];
// Wake up collectionView
[self.collectionView reloadData];
}
-(void)createCollectionView {
// Layout
UICollectionViewFlowLayout *collectionViewLayout = [[UICollectionViewFlowLayout alloc] init];
collectionViewLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
// UICollectionView
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:collectionViewLayout];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
[self.view addSubview:self.collectionView];
// Behavior
self.collectionView.pagingEnabled = YES;
// Appearance
self.collectionView.backgroundColor = [Color colorWithName:nil alpha:0.2f];
self.collectionView.showsHorizontalScrollIndicator = NO;
// Register cells
//[self.collectionView registerClass:[TrackingSetCollectionViewCell class] forCellWithReuseIdentifier:TrackingSetCollectionViewCellIdentifier]; }
-(void)createPageView {
// Support for pagination - DAPageControlView
self.pageControlView = [[DAPageControlView alloc] initWithFrame:CGRectZero];
self.pageControlView.delegate = self;
[self.view addSubview:self.pageControlView];
self.pageControlView.hidden = YES; // do not show the dots
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return fetchManagedObjectsFromEntity(#"Autor", #[[NSSortDescriptor sortDescriptorWithKey:#"autorID" ascending:YES]], nil, self.managedObjectContext).count + 1; // +1 for testing !
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//test
UICollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:#"cell" forIndexPath:indexPath];
if (indexPath.row==0) {
cell.backgroundColor = [UIColor blueColor];
}
if (indexPath.row==1) {
cell.backgroundColor = [UIColor redColor];
}
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return self.collectionView.bounds.size;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 0.0;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0, 0, 0, 0);
}
#pragma mark - DAPageControlView Delegate
- (void)pageControlViewDidChangeCurrentPage:(DAPageControlView *)pageControlView
{
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:pageControlView.currentPage inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
}

Problem solved - I forgot to register a class for the UICollectionViewCell. I thought it was not necessary for to do so for the 'default' class UICollectionViewCell but since the UICollectionView is added programmatically, it makes sense.

Related

How to remove spacing between lines in UICollectionView programmatically?

I have a UICollectionView and I can't get 0 spacing between lines. I have added UICollectionViewDelegateFlowLayout in .h class and implemented
(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
and
(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section**
methods set them to return 0.0. Also in ViewDidLoad I dynamically created UICollectionViewFlowLayout and UICollectionView. For UICollectionViewFlowLayout I set minimumInteritemSpacing and minimumLineSpacing to 0 but space between lines is still more than 0.
This is my code:
- (void)initializeCollectionView {
UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc]init];
flowLayout.itemSize = CGSizeMake(50, 100);
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[flowLayout setMinimumLineSpacing:0.0];
flowLayout.minimumInteritemSpacing = 0;
flowLayout.minimumLineSpacing = 0;
collView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height-70) collectionViewLayout:flowLayout];
[collView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"Cell"];
[collView setBackgroundColor:[UIColor grayColor]];
collView.delegate = self;
collView.dataSource = self;
[[self view] addSubview:collView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 100;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 0.0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 0.0;
}
- (UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0, 0, 0, 0);
}
Thank you for your answers.
I did it in a little different way. I created a collectionView programatically and added that to the view Controller. In my CollectionView Custom Class, I did all the customisation.
My Custom Collection View interface file-
#import <UIKit/UIKit.h>
#import "CustomCell.h"
#interface CustomCollectionView : UIView<UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UICollectionViewDelegate>
#property(nonatomic, strong) UICollectionView *myCollectionView;
#end
Now in the implementation File(CustomCollectionView.m), first I had to initialise and add my collectionView.
initWithFrame is called as soon as the view is drawn. So, I added my collectionView inside this view and let this view deal with all the delegate methods to customise my collection view and populate data through datasource methods.
-(id)initWithFrame:(CGRect)frame{
self=[super initWithFrame:frame];
if(self){
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
self.myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 20, frame.size.width, frame.size.height) collectionViewLayout:flowLayout];
self.myCollectionView.backgroundColor=[UIColor whiteColor];
self.myCollectionView.delegate=self;
self.myCollectionView.dataSource=self;
[self.myCollectionView registerClass:[CustomCell class] forCellWithReuseIdentifier:#"customCell"];
[self addSubview:self.myCollectionView];
}
return self;
}
Now, let's implememt the UICollectionViewDelegateFlowLayout methods
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 0;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
//here I just made sure that I have square spaces boxes as my cell size
int width = self.myCollectionView.frame.size.width/5;
int height = width;
return CGSizeMake(width, height);
}
Now the UICollectionView Datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 5;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 7;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CustomCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:#"customCell" forIndexPath:indexPath];
if(cell){
//customize the cell here
return cell;
}
}
Now when my custom view with collectionView is ready, I just added that to my viewController.
- (void)viewDidLoad {
[super viewDidLoad];
CustomCollectionView *myCollectionView = [[CustomCollectionView alloc]initWithFrame:CGRectMake(0, 10, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview: myCollectionView];
}
The above worked for me. You can do with storyboard to, but I found some complication when I tried to customise a collectionView connected by IBOutlet. So, I took this approach. There may be better ways to do that.

Keep reference of views from collectionView is doubled

Trying to keep reference in arrays of all views that being added to collection view .
So what happens, is that i have this array with the data, but when i scroll down the collection, it calls the reusable cells function ,and try to add them again to my array ,although i am checking if they are there before adding them again :
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
//quantity
UILabel *quantityL=[[UILabel alloc] initWithFrame:CGRectMake(cell.frame.size.width/10,cell.frame.size.width/10, cell.frame.size.width/5,cell.frame.size.width/5)];
quantityL.text=[NSString stringWithFormat:#"%d",quantity];
quantityL.font=[UIFont fontWithName:[Globals sharedGlobals].titleFont size:[Globals sharedGlobals].badgeSize];
quantityL.textAlignment=NSTextAlignmentCenter;
//more and more stuff
[cell addSubview:quantityL]; //add to cell
if(![allQuantities containsObject:quantityL]) //check if already in array!
[allQuantities addObject:quantityL]; //add to array
i can see that allQuantities array is changing its size... why ?
To properly reuse and set frames: code for your controller:
#define kMyCellIdentifier #"kMyCellIdentifier"
- (void)viewDidLoad {
[super viewDidLoad];
//...
[self.collectionView setDelegate:self];
[self.collectionView setDataSource:self];
[self.collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:kMyCellIdentifier];
}
#pragma mark - UICollectionViewDelegate && UICollectionViewDataSource
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kMyCellIdentifier forIndexPath:indexPath];
[cell.textLabel setText:#"blah"];
return cell;
}
And your cell subclass:
#interface MyCollectionViewCell : UICollectionViewCell
#property(nonatomic, readonly) UILabel *textLabel;
#end
#implementation MyCollectionViewCell
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
_textLabel = [[UILabel alloc] init];
[self.textLabel setTextAlignment:NSTextAlignmentCenter];
[self.contentView addSubview:self.textLabel];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect rect = self.contentView.bounds;
[self.textLabel setFrame:rect];
}

My UICollectionView does not call its datasource methods?

I've just got a standard UIViewController subclass with a UICollectionView. That UICollection view not calling its datasource (or delegate) methods, but I can't see which piece is missing.
Here's the interface for my UIViewController class that presents the UICollectionView:
#interface PastViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
...
#property (nonatomic, weak) UICollectionView *collectionView;
#end
Here is my viewDidLoad (almost) in its entirety:
- (void)viewDidLoad {
[super viewDidLoad];
UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc]init];
flowLayout.itemSize = CGSizeMake(100, 100);
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
//self.complaints is used to generate the collection view cells
//the log always prints out 30 complaints
self.complaints = [self.complaintDatabase getComplaints ];
NSLog(#"got a total OF %d complaints", self.complaints.count);
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:flowLayout];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView registerClass:[VideoCollectionViewCell class] forCellWithReuseIdentifier:#"cell"];
[self.view addSubview:self.collectionView];
[self.collectionView reloadData];
[self.collectionView reloadInputViews];
}
I'll also post the datasource, delegate, and delegate flow layout protocols at the end. There are no other subviews added to the view controller, and I have set the delegate and declared the delegate protocol, so the following SO posts don't apply:
UICollectionView doesn't call delegate methods
UIcollection view not getting call datasources
What am I missing? Why would the data source methods (and delegate methods) never be called?
Here are the methods implemented for the various protocols:
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
NSLog(#"should be seeing %d collection view cells", self.complaints.count);
return self.complaints.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
NSLog(#"QUERYING NUMBER OF SECTIONS IN COLLECTION VIEW");
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
VideoCollectionViewCell *cell = [[VideoCollectionViewCell alloc] initWithCoder:#"cell"];
cell.complaint = self.complaints[indexPath.row];
return cell;
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(#"calling didselctitem at path");
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(#"diddeselectitem");
}
#pragma mar - UICollectionViewFlowDelegateLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(100, 100);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(50, 20, 50, 20);
}
#property (nonatomic, strong) UICollectionView *collectionView;
Change weak to strong, or collectionView will be released after assignment, which means collectionView will be nil so that the data source methods would never be called.
Another approach:
Keep collectionView to be weak.
- (void)viewDidLoad {
[super viewDidLoad];
UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc]init];
flowLayout.itemSize = CGSizeMake(100, 100);
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
//self.complaints is used to generate the collection view cells
//the log always prints out 30 complaints
self.complaints = [self.complaintDatabase getComplaints ];
NSLog(#"got a total OF %d complaints", self.complaints.count);
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:flowLayout];
collectionView.delegate = self;
collectionView.dataSource = self;
[collectionView registerClass:[VideoCollectionViewCell class] forCellWithReuseIdentifier:#"cell"];
[self.view addSubview:collectionView];
self.collectionView = collectionView;
[self.collectionView reloadData];
[self.collectionView reloadInputViews];
}
See: Creating views programmatically Strong Vs Weak subviews in controller

Why is my UICollectionViewCell touch event offset ?

I have a UICollectionView that is using the Flow Layout. At this point I'm not really doing anything special. I'm just programmatically adding the view and the UICollectionViewCell.
When you touch a cell it is registering the touch event for the cell that is located above it. So if you touch a cell on row 2 the the cell above is the one that actually registers the touch.
Here is the code that I have setting things up.
#interface KFYSearch() <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UISearchBarDelegate>
#property(nonatomic, strong)UISearchBar *searchBar;
#property(nonatomic, strong)UICollectionView *collectionView;
#property(nonatomic, strong)NSArray *collectionData;
#end
#implementation KFYSearch
-(id)initWithView:(UIView*)view
{
self = [super init];
if(self)
self.view = view;
return self;
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if(_searchBar == nil)
{
self.view.frame = CGRectMake(20, 20, self.view.superview.frame.size.width - 40, self.view.superview.frame.size.height - 40);
[self.view setBackgroundColor:[UIColor whiteColor]];
[self createChildViews];
}
}
-(void)createChildViews
{
_searchBar = [[UISearchBar alloc] initWithFrame: CGRectMake(5, 5, self.view.frame.size.width - 10, 30)];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(_searchBar.frame.origin.x, _searchBar.frame.origin.y + (_searchBar.frame.size.height + 2), _searchBar.frame.size.width, self.view.frame.size.height - ((_searchBar.frame.origin.y + _searchBar.frame.size.height) + 10)) collectionViewLayout:flowLayout];
_collectionView.dataSource = self;
_collectionView.delegate = self;
[_collectionView registerClass:[KFYKlickableCell class] forCellWithReuseIdentifier:#"Cell"];
[_collectionView setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:_searchBar];
[self.view addSubview:_collectionView];
[self getData];
}
-(void)getData
{
KFYGetKlickablesAction *getKlickablesAction = [[KFYGetKlickablesAction alloc]init];
_collectionData = [getKlickablesAction mockData];
[_collectionView reloadData];
}
#pragma UICollectionView Delegate stuff;
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return _collectionData.count;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
KFYKlickableCell *cell= [_collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
[cell setBackgroundColor:[UIColor redColor]];
KFYKlickableVO *vo = [_collectionData objectAtIndex:indexPath.row];
[cell renderKlickable:vo];
return (UICollectionViewCell*)cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(60, 60);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(5, 5, 5, 5);
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
KFYKlickableVO *vo = [_collectionData objectAtIndex:indexPath.row];
NSLog(vo.tag);
}
#pragma UITextField Delegate stuff
-(void)textFieldDidBeginEditing:(UITextField *)textField {
//Keyboard becomes visible
self.view.frame = CGRectMake(self.view.frame.origin.x,
self.view.frame.origin.y,
self.view.frame.size.width,
self.view.frame.size.height - 215 + 50); //resize
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
//keyboard will hide
self.view.frame = CGRectMake(self.view.frame.origin.x,
self.view.frame.origin.y,
self.view.frame.size.width,
self.view.frame.size.height + 215 - 50); //resize
}
#end

Is it feasible to create custom CollectionViewLayout where spacing between individual cell can specifically control

I am trying to build a honeycomb using UiCollectionView . I have used https://github.com/cyrilchandelier/CCHexagonFlowLayout component for creating the basic structure of my view.
According to the requirement when any cell is tapped the adjacent cells should move out distance x from the tapped cell, with animation .
I have create two collectionViewLayout with different spacing value and changing it at didSelectItemAtIndexPath method
Here is my code …
#import "HoneycombViewController.h"
#interface HoneycombViewController ()
// Collection view
#property (nonatomic, strong) UICollectionView *collectionView;
#property(nonatomic,strong) CCHexagonFlowLayout *layout;
#property(nonatomic,strong) CCHexagonFlowLayout *largeLayout;
#property(nonatomic) CGFloat minimumItemSpacingFactor;
#property(nonatomic) CGFloat minimumLineSpacingFactor ;
#property(nonatomic) CGFloat layoutGapFactor ;
#end
#implementation A3HoneycombViewController
#synthesize minimumLineSpacingFactor,minimumItemSpacingFactor,layoutGapFactor;
- (void)viewDidLoad
{
[super viewDidLoad];
self.minimumItemSpacingFactor = 10.0f;
self.minimumLineSpacingFactor = 10.0f;
self.layoutGapFactor = 10.0f;
// Build layout
self.layout = [[CCHexagonFlowLayout alloc] init];
self.layout.delegate = self;
//layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.layout.scrollDirection = UICollectionViewScrollDirectionVertical;
self.layout.minimumInteritemSpacing = -30.0f;
self.layout.minimumLineSpacing = 10.0f;
self.layout.sectionInset = UIEdgeInsetsMake(20.0f, 15.0f, 20.0f, 15.0f);
self.layout.itemSize = RootCell_SIZE;
//layout.headerReferenceSize = HeaderView_SIZE(layout.scrollDirection);
//layout.footerReferenceSize = FooterView_SIZE(layout.scrollDirection);
self.layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width,50) ;
self.layout.footerReferenceSize = CGSizeMake(self.view.frame.size.width,50) ;
self.layout.gap = 76.0f;
//create large layout
self.largeLayout = [[CCHexagonFlowLayout alloc] init];
self.largeLayout.delegate = self;
self.largeLayout.scrollDirection = self.layout.scrollDirection;
self.largeLayout.minimumLineSpacing = self.layout.minimumLineSpacing+self.minimumLineSpacingFactor;
self.largeLayout.minimumInteritemSpacing = self.layout.minimumInteritemSpacing+self.minimumItemSpacingFactor;
self.largeLayout.itemSize = self.layout.itemSize;
self.largeLayout.sectionInset = self.layout.sectionInset;
self.largeLayout.headerReferenceSize =self.layout.headerReferenceSize;
self.largeLayout.footerReferenceSize = self.layout.footerReferenceSize;
self.largeLayout.gap = self.layout.gap + self.layoutGapFactor;
// Build collection view
_collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:self.layout];
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.backgroundColor = [UIColor whiteColor];
// Register cell and views
[_collectionView registerNib:[RootCell cellNib] forCellWithReuseIdentifier:RootCell_ID];
[_collectionView registerNib:[HeaderView viewNib] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeaderView_ID];
[_collectionView registerNib:[FooterView viewNib] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:FooterView_ID];
// Add to view
[self.view addSubview:_collectionView];
// Reload data
[_collectionView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UICollectionDataSource methods
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
switch (section)
{
case 0:
return 10;
default:
return 10;
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
RootCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:RootCell_ID forIndexPath:indexPath];
[cell configureWithInt:indexPath.item];
return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath
{
// Section header
if ([kind isEqualToString:UICollectionElementKindSectionHeader])
return [_collectionView dequeueReusableSupplementaryViewOfKind:kind
withReuseIdentifier:HeaderView_ID
forIndexPath:indexPath];
// Section footer
if ([kind isEqualToString:UICollectionElementKindSectionFooter])
return [_collectionView dequeueReusableSupplementaryViewOfKind:kind
withReuseIdentifier:FooterView_ID
forIndexPath:indexPath];
return nil;
}
-(CGSize)collectionViewContentSize { //Workaround
CGSize superSize = [self.layout collectionViewContentSize];
CGRect frame = self.collectionView.frame;
return CGSizeMake(fmaxf(superSize.width, CGRectGetWidth(frame)), fmaxf(superSize.height, CGRectGetHeight(frame)));
}
//
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewLayout *toLayout = self.layout == self.collectionView.collectionViewLayout ? self.largeLayout : self.layout;
[self.collectionView setCollectionViewLayout:toLayout animated:YES completion:^(BOOL finished) {
NSLog(#"animation end ");
}];
}
In the screenshot2 I have tapped in cell 1 now adjacent cells like 0 ,5,6,7,2 moved correctly as marked in green but other cells also moved marked in red .
As per this code all cell layout is changed but I want only my tabbed cell should be stick to its position and change its cell spacing show that it seems like its adjacent cells are move away at distance x also other cells in between space should remain with older value. Is it feasible to create custom CollectionViewLayout where spacing between individual cell can specifically control ? Also how we can use different CollectionViewLayout for different cells?

Resources