UICollectionView cell leaves huge gaps all settings set to 0 - ios

Hi there i have a slight problem with my UICollectionView. i don't currently have any settings set for spacing yet i seem to have a huge gap between my cells of which is very annoying if would be helpful if someone could tell me how to resolve this? id assume its something very simple.
Below is an example of whats happening to my CollectionViewCells:
Custom cell.h
#import <UIKit/UIKit.h>
#interface CustomCell : UICollectionViewCell
#property (weak, nonatomic) IBOutlet UIImageView *IconImage;
#property (weak, nonatomic) IBOutlet UILabel *IconLabel;
#property (weak, nonatomic) IBOutlet UILabel *IconDescription;
#end
groupsviewcontroller.m
#import "GroupsViewController.h"
#import "CustomCell.h"
#interface GroupsViewController ()
{
NSArray *arrayOfImages;
NSArray *arrayOfDescriptions;
}
#end
#implementation GroupsViewController
{
NSString *reuseIdentifier;
}
- (void)viewDidLoad
{
[super viewDidLoad];
reuseIdentifier= #"SmallIcon";
[[self GroupsCollectionView]setDataSource:self];
[[self GroupsCollectionView]setDelegate:self];
arrayOfImages = [[NSArray alloc]initWithObjects:#"sin.png", nil];
arrayOfDescriptions = [[NSArray alloc]initWithObjects:#"Sin", nil];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [arrayOfDescriptions count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
[[cell IconImage]setImage:[UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]]];
[[cell IconLabel]setText:[arrayOfDescriptions objectAtIndex:indexPath.item]];
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
//Dispose of any resources that can be recreated.
}
- (IBAction)cellToggleAction:(id)sender {
//need to add toggle button to toggle between three different views
//small icon
//list view
//large icon
}
#end

1) You should check your CustomCell class, see if any size constraint is applied. Simply set your cell's background as some color and the actual cell size will be highlighted.
2) Use (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath to help yourself set the proper cell size. This func comes with UICollectionViewDelegateFlowLayout :)

Related

UICollectionView crashes when scrolling to the bottom

I have a UICollectionView that instantiates a bunch of images in a Flow Layout. The problem I have is when I scroll to the bottom of the view the app crashes and gives the following error: "*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[StickerPickerViewCell setImage:]: unrecognized selector sent to instance 0x101bdf470'". This seems odd to me because I don't know why it would be trying to add more images when you are at the bottom. Here is the code I have:
#import <UIKit/UIKit.h>
#interface StickerPickerViewCell : UICollectionViewCell
#property (weak, nonatomic) IBOutlet UILabel *testText;
#property (weak, nonatomic) IBOutlet UIImageView *stickerImage;
//- (void)setStickerImage:(StickerImage *)sticker;
#end
#import <Foundation/Foundation.h>
#import "StickerPickerViewCell.h"
#implementation StickerPickerViewCell
/*
- (void)setStickerImage:(StickerImage *)sticker{
{
self.stickerImage.image = [UIImage imageNamed:sticker.stickerImage];
}
*/
#end
#import <UIKit/UIKit.h>
#import "PhotoEditViewController.h"
#interface StickerPickerViewController : UIViewController <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
//method declarations
- (void)viewDidLoad;
#property (weak, nonatomic) id<StickerPickerDelegate> pickerDelegate;
#property (weak, nonatomic) IBOutlet UICollectionView *stickerView;// sticker picker collection view
#property (nonatomic, strong) IBOutlet UICollectionViewFlowLayout *flowLayout;
#end
#import <Foundation/Foundation.h>
#import "StickerPickerViewController.h"
#import "StickerPickerViewCell.h"
#implementation StickerPickerViewController
//holds all the locations to the stickers we want to appear
- (void)viewDidLoad {
[super viewDidLoad];
[self.stickerView setBackgroundColor:[UIColor whiteColor]];
//flowlayout is how the cells organize around one another in the collectionview
self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
[self.flowLayout setItemSize:CGSizeMake(40, 40)];
[self.flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
self.flowLayout.minimumInteritemSpacing = 0.0f;
[self.stickerView setCollectionViewLayout:self.flowLayout];
self.stickerView.bounces = YES;
[self.stickerView setShowsHorizontalScrollIndicator:NO];
[self.stickerView setShowsVerticalScrollIndicator:YES];
}
-(void)viewDidAppear:(BOOL)animated{
}
//////////////////////////////////////////////////COLLECTION VIEW METHODS ///////////////////////////////////////////////////////////////////
//number of cells to produce
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
//return list of sticker's size
//printf("i: %lu", (unsigned long)[stickerList count]);
StickerManager* stickerManager = [self.pickerDelegate stickerManagerForStickers];
return stickerManager.stickers.count;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
StickerManager* stickerManager = [self.pickerDelegate stickerManagerForStickers];
Sticker* sticker = stickerManager.stickers[indexPath.row];
//create cell
StickerPickerViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"stickerPickerViewCell" forIndexPath:indexPath];
//set cell image to image associated with the path in the array
cell.stickerImage.image = sticker.preview;
cell.stickerImage = (UIImageView *)[cell viewWithTag:100];
return cell;
}
//cell size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(75, 75);
}
//number of cell sections
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//get the selected image as String
Sticker* selectedSticker = [self.pickerDelegate stickerManagerForStickers].stickers[indexPath.row];
[self dismissViewControllerAnimated:YES completion:nil];
[self.pickerDelegate didSelectSticker:selectedSticker];
//dismiss popover
[self dismissViewControllerAnimated:true completion:nil];
}
#end
What could be causing this crash?

UIController View error

I created an array of images and CollectionView for displaying 9 images grid. But there is an error while using the image outlet and CollectionView array.I declared it by name myImage and myDescriptionLabel for image inside the cell and label respectively. I have indicated the error by commenting it.
CollectionViewController.h file :-
#import <UIKit/UIKit.h>
#interface CollectionViewController : UICollectionViewController
#property (strong, nonatomic) IBOutlet UICollectionView *myCollectionView;
#property (strong, nonatomic) IBOutlet UIImageView *myImage;
#property (strong, nonatomic) IBOutlet UILabel *myDescriptionLabel;
#end
CollectionViewController.h file :-
#import "CollectionViewController.h"
#interface CollectionViewController ()
{
NSArray *arrayofImages;
NSArray *arrayofDescription;
}
#end
#implementation CollectionViewController
static NSString * const reuseIdentifier = #"Cell";
- (void)viewDidLoad {
[super viewDidLoad];
[[self myCollectionView]setDataSource:self];
[[self myCollectionView]setDelegate:self];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
arrayofImages=[[NSArray alloc]initWithObjects:#"1.jpeg",#"2.jpeg",#"3.jpeg",#"4.jpg",#"5.jpg",#"6.jpg",#"7.jpeg",#"8.jpg",#"10.jpg", nil];
arrayofDescription = [[NSArray alloc]initWithObjects:#"Image 1",#"Image 2",#"Image 3", #"Image 4",#"Image 5",#"Image 6", #"Image 7", #"Image 8",#"Image 9", nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [arrayofDescription count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
[[cell myImage]setImage:[UIImage imageNamed:[arrayofImages object_getIndex:index.item]]];
[[cell myDescriptionLabel]setText:[arrayofDescription object_getIndex:IndexPath.item]];
// Here i Am getting error
return cell;
}
#pragma mark <UICollectionViewDelegate>
#end
try this may be you are wrong here:-
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
[[cell self.myImage]setImage:[UIImage imageNamed:[arrayofImages objectAtIndex:index.item]]];
[[cell self.myDescriptionLabel]setText:[arrayofDescription objectAtIndex:IndexPath.item]];
// Here i Am getting error
return cell;
}

Toggle cell size button not changing cell identifier UIViewController

i have created some code in my button to toggle between my cell identifier of which does so pretty well but obviously i needed to set and initial cell identifier of which is small icon, so how would i go about remove that cell identifier and replacing it with another once the button is clicked. My current code is as follows:
GroupsViewController.m
#import "GroupsViewController.h"
#import "CustomCell.h"
#interface GroupsViewController ()
{
NSArray *arrayOfImages;
NSArray *arrayOfDescriptions;
}
#end
#implementation GroupsViewController
{
NSString *reuseIdentifier;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[self GroupsCollectionView]setDataSource:self];
[[self GroupsCollectionView]setDelegate:self];
reuseIdentifier= #"SmallIcon";
arrayOfImages = [[NSArray alloc]initWithObjects:#"?.png", nil];
arrayOfDescriptions = [[NSArray alloc]initWithObjects:#"?", nil];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [arrayOfDescriptions count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
[[cell IconImage]setImage:[UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]]];
[[cell IconLabel]setText:[arrayOfDescriptions objectAtIndex:indexPath.item]];
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
//Dispose of any resources that can be recreated.
}
- (IBAction)cellToggleAction:(id)sender {
if([reuseIdentifier isEqualToString:#"SmallIcon"])
reuseIdentifier=#"ListView";
else if
([reuseIdentifier isEqualToString:#"ListView"])
reuseIdentifier=#"LargeIcon";
else if
([reuseIdentifier isEqualToString:#"LargeIcon"])
reuseIdentifier=#"SmallIcon";
[self.GroupsCollectionView reloadData];
}
#end
CustomCell.h
#import <UIKit/UIKit.h>
#interface CustomCell : UICollectionViewCell
#property (weak, nonatomic) IBOutlet UIImageView *IconImage;
#property (weak, nonatomic) IBOutlet UILabel *IconLabel;
#end
I assume its to do with me setting the reuseIdentifier in the
- (void)viewDidLoad so that i didn't get any errors so that i hadn't set one, so really what i am asking for is a way to set the initial reuseidzntifier and replace it will the following when i toggle between the button clicks.
Also it would be helpful if someone could point me in the right direction as to adding icon images to each click of the button.
The problem happens when i am clicking the button as shown in the following images, the cells themselves change but the initial cell identifier stays put.
From what I understand your UICollectionViewCells are working fine. You just need to adjust their size when cells are toggled.
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGSize cellSize;
// Return required size based on your identifiers
if([reuseIdentifier isEqualToString:#"SmallIcon"])
cellSize = CGSizeMake(50, 50); // Sample size
else if
([reuseIdentifier isEqualToString:#"ListView"])
cellSize = CGSizeMake(80, 80); // Sample size
else if
([reuseIdentifier isEqualToString:#"LargeIcon"])
cellSize = CGSizeMake(120, 120); // Sample size
return cellSize;
}

UiCollcetionViewCell specific segue to connect TableView show specific content within TableView

Hi there the question i would like to ask is there a way of creating such code in Objective C that will allow me to select a single UICollectionViewCell within a UICollectionViewController and link it to a table view that shows UICollectionViewCell specific content based upon what UICollectionViewCell has been selected without creating hundreds of UITableViewControllers?
A know this is not code but this is what i want the code to achieve if it is possible:
on click of collection view cell dresses show table view.
then on the table view...
if collection view clicked was dresses display dresses table view.
surely this is possible?
Also if possible i would like the code to somehow group things togethers as this will be done on a large scale i have 106 collection view cells that need linking to table views of which need to contain a minimum of 30 TableViewCells each.
I tried your question for getting solution.Finally I successfully got the solution.Below code works perfectly.
In ViewController
.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>
#property (strong, nonatomic) IBOutlet UICollectionView *collectionViewSelection;
#end
.m
#import "ViewController.h"
#import "CustomCollectionViewCell.h"
#import "DetailViewController.h"
#interface ViewController ()
{
NSMutableArray *arrayCollectionView;
NSMutableArray *imgArray;
NSMutableArray *lblArray;
}
#end
#implementation ViewController
#synthesize collectionViewSelection;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
imgArray = [NSMutableArray arrayWithObjects:
[UIImage imageNamed:#"casual.png"],
[UIImage imageNamed:#"checked.png"],
[UIImage imageNamed:#"collar.png"],
[UIImage imageNamed:#"formal.png"],
[UIImage imageNamed:#"jean.png"],
[UIImage imageNamed:#"neck.png"],
[UIImage imageNamed:#"pant.png"],nil];
lblArray = [NSMutableArray arrayWithObjects:#"casual",#"checked",#"collar",#"formal",#"jean",#"neck",#"pant", nil];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
UINib *cellNib = [UINib nibWithNibName:#"CustomCollectionViewCell" bundle:nil];
[collectionViewSelection registerNib:cellNib forCellWithReuseIdentifier:#"customCollectionCell"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [lblArray count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"customCollectionCell";
CustomCollectionViewCell *cell = (CustomCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
NSLog(#"The current indexPath row is - %ld",(long)indexPath.row);
cell.img_Collection.image = [imgArray objectAtIndex:indexPath.row];
cell.label_Collection.text = [lblArray objectAtIndex:indexPath.row];
cell.tag = indexPath.row;
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(260, 176);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"The touched index path os collection cell item row is - %ld",(long)indexPath.row);
DetailViewController *detailsVC = [[DetailViewController alloc]initWithNibName:#"DetailViewController" bundle:nil];
detailsVC.stringLabeldata = [lblArray objectAtIndex:indexPath.row];
detailsVC.imageData = [imgArray objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailsVC animated:YES];
}
#end
DetailViewController
.h
#import <UIKit/UIKit.h>
#interface DetailViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
#property (strong, nonatomic) IBOutlet UITableView *tableViewDressesData;
#property (strong, nonatomic) UIImage *imageData;
#property (strong, nonatomic) NSString *stringLabeldata;
- (IBAction)actionBack:(id)sender;
#end
.m
#import "DetailViewController.h"
#import "CustomTableViewCell.h"
#interface DetailViewController ()
#end
#implementation DetailViewController
#synthesize tableViewDressesData;
#synthesize imageData,stringLabeldata;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSLog(#"The labeldata is -%#",stringLabeldata);
[tableViewDressesData registerNib:[UINib nibWithNibName:#"CustomTableViewCell" bundle:nil] forCellReuseIdentifier:#"cell"];
[tableViewDressesData reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.imgViewDetail.image = imageData;
cell.labelDetail.text = stringLabeldata;
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 188;
}
- (IBAction)actionBack:(id)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
#end

RFQuiltLayout and UICollectionView margin

I am trying to use RFQuiltLayout in my Collection View and I keep running into an size/margin issue. It seems appears fine using the 4s and 5s simulator but when I test it on the 6 or 6plus, the blocks get pushed over to the left side of the screen and leave a large margin on the right.
Not sure what's going on here.
image of problem
storyboard
Here is the code for the collection view controller
.h
#import "RFQuiltLayout.h"
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UICollectionViewDataSource, UICollectionViewDelegate, RFQuiltLayoutDelegate, UICollectionViewDelegateFlowLayout>
#property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
#property (nonatomic, strong) NSArray *greekLetters;
.m
#import "ViewController.h"
#import "CollectionViewCell.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
RFQuiltLayout* layout = (id)[self.collectionView collectionViewLayout];
layout.direction = UICollectionViewScrollDirectionVertical;
layout.blockPixels = CGSizeMake(75, 75);
self.greekLetters = #[#"Alpha", #"Beta", #"Cappa",#"Delta", #"Epsilon", #"Zeta", #"Eta", #"Theta", #"Iota", #"Kappa", #"Lambda", #"Mu", #"Nu", #"Xi", #"omicron", #"pi",#"rho",#"sigma", #"tau", #"upsilon", #"phi", #"chi",#"psi",#"omega"];
// [[self collectionView]setDataSource:self];
// [[self collectionView]setDelegate:self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma - mark CollectionView DelegateMethods:
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.greekLetters.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
NSString *CellIdentifer = #"cell";
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifer forIndexPath:indexPath];
cell.cellLabel.text = [self.greekLetters objectAtIndex:indexPath.row];
return cell;
}
#pragma mark – RFQuiltLayoutDelegate
-(CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout blockSizeForItemAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row %2) {
return CGSizeMake(2, 3);
}else{
return CGSizeMake(2, 2);
}
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetsForItemAtIndexPath:(NSIndexPath *)indexPath {
return UIEdgeInsetsMake(5, 5, 5, 5);
}
#end
Not sure what could be causing it.
Any help is greatly appreciated.
thanks.
just play with the blockpixels
for mine these values work perfectly
layout.blockPixels = CGSizeMake(106.66, 106.66);

Resources