UICollectionView Crashes when Cell is Touched - ios

I have a UICollectionView with a custom class subclassed to UICollectionViewCell showing pictures from within the app. It loads fine, but if any cell is even barely touched, the app crashes. There is no error message in the Console, and nothing shows if I put in an Exception breakpoint either. The code for the view controller containing the Collection View is:
#import "ImagePicker.h"
#import "CMFGalleryCell.h"
#interface ImagePicker ()
#property (nonatomic, weak) IBOutlet UICollectionView *collectionView;
#property (nonatomic, strong) NSArray *dataArray;
#end
#implementation ImagePicker
#synthesize dataArray, collectionView;
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Choose Image";
[self loadImages];
[self setupCollectionView];
// Do any additional setup after loading the view from its nib.
}
-(void)setupCollectionView {
[self.collectionView registerClass:[CMFGalleryCell class] forCellWithReuseIdentifier:#"cellIdentifier"];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
[flowLayout setMinimumInteritemSpacing:0.0f];
[flowLayout setMinimumLineSpacing:0.0f];
[self.collectionView setPagingEnabled:YES];
[self.collectionView setCollectionViewLayout:flowLayout];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self.dataArray count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CMFGalleryCell *cell = (CMFGalleryCell *)[collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
NSString *imageName = [self.dataArray objectAtIndex:indexPath.row];
[cell setImageName:imageName];
[cell updateCell];
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Chose%ld", (long)indexPath.row);
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(185, 185);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 1.0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 1.0;
}
// Layout: Set Edges
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
// return UIEdgeInsetsMake(0,8,0,8); // top, left, bottom, right
return UIEdgeInsetsMake(0,0,0,0); // top, left, bottom, right
}
-(void)loadImages {
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"inviteimages"];
self.dataArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:sourcePath error:NULL];
NSLog(#"%#", self.dataArray);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
The custom Cell is:
#import "CMFGalleryCell.h"
#interface CMFGalleryCell()
#property (strong, nonatomic) IBOutlet UIImageView *imageView;
#end
#implementation CMFGalleryCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:#"CMFGalleryCell" owner:self options:nil];
if ([arrayOfViews count] < 1) {
return nil;
}
if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) {
return nil;
}
self = [arrayOfViews objectAtIndex:0];
}
return self;
}
-(void)updateCell {
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"inviteimages"];
NSString *filename = [NSString stringWithFormat:#"%#/%#", sourcePath, self.imageName];
UIImage *image = [UIImage imageWithContentsOfFile:filename];
[self.imageView setImage:image];
[self.imageView setContentMode:UIViewContentModeScaleAspectFill];
}
#end
What is going on causing it to crash if a cell is touched? If I touch the space between cells, I can get it to scroll, but any touch at all of the cell crashes.
Screenshot of crash without zombies:
Screenshot of crash with zombies:

You said you app will crash when cell is touched but you haven't implemented the cell select delegate method but written only cell deselect delegate method. Try this
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
And insert a breakpoint inside the same method to debug. If break point is not called then something else is the problem

Maybe problem is in your View Controller class:
#property link to ImagePicker can be weak, and you, for example, remove it from superview. After this, ImagePicker object will be deallocated.
Try to replace
#property (weak, nonatomic) ImagePicker *someImagePicker
with
#property (strong, nonatomic) ImagePicker *someImagePicker
Can you share code from your UIViewController?

Related

Collection View Cell Size Is Not As Expected

I'm trying to make a custom keyboard using storyboard. I need to use two collection views. I want the height and width of collection view cell as the height of collection view itself. Constraints are set properly. But cell size is not looking as expected. It is same as what height collection view has on the storyboard.
Here is the code.
#import "MainViewController.h"
#import "UpperCollectionViewCell.h"
#import "LowerCollectionViewCell.h"
#interface MainViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
#property (weak, nonatomic) IBOutlet UICollectionView *upperCollectionView;
#property (weak, nonatomic) IBOutlet UICollectionView *lowerCollectionView;
#property (strong, nonatomic) NSArray *upperData;
#property (strong, nonatomic) NSArray *lowerData;
#end
#implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"View Presented");
self.lowerCollectionView.delegate = self;
self.lowerCollectionView.dataSource = self;
self.upperCollectionView.delegate = self;
self.upperCollectionView.dataSource = self;
self.upperData = #[[[DataModel alloc] initWithTitle:#"Bollywood" subtitle:nil color:[UIColor orangeColor]],
[[DataModel alloc] initWithTitle:#"Hollywood" subtitle:nil color:[UIColor greenColor]],
[[DataModel alloc] initWithTitle:#"Tollywood" subtitle:nil color:[UIColor magentaColor]]];
self.lowerData = #[[[DataModel alloc] initWithTitle:#"News" subtitle:#"Very first comment" color:[UIColor blueColor]],
[[DataModel alloc] initWithTitle:#"Tasks" subtitle:#"Second comment" color:[UIColor redColor]],
[[DataModel alloc] initWithTitle:#"Events" subtitle:#"Third comment" color:[UIColor greenColor]]];
}
- (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 {
if(collectionView == self.lowerCollectionView) {
return self.lowerData.count;
}
else if(collectionView == self.upperCollectionView) {
return self.upperData.count;
}
else {
return 0;
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if(collectionView == self.lowerCollectionView) {
LowerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"lower_cv_cell" forIndexPath:indexPath];
[cell setDataModel:self.lowerData[indexPath.row]];
[cell layoutIfNeeded];
return cell;
}
else if(collectionView == self.upperCollectionView) {
UpperCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"upper_cv_cell" forIndexPath:indexPath];
[cell setDataModel:self.upperData[indexPath.row]];
[cell layoutIfNeeded];
return cell;
}
else {
return nil;
}
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
if(collectionView == self.lowerCollectionView) {
CGSize size = CGSizeMake(collectionView.bounds.size.height, collectionView.bounds.size.height-10);
NSLog(#"lower height : %#",NSStringFromCGSize(size));
return size;
}
else if(collectionView == self.upperCollectionView) {
CGSize size = CGSizeMake(collectionView.bounds.size.height, collectionView.bounds.size.height-10);
NSLog(#"upper height : %#",NSStringFromCGSize(size));
return size;
}
else {
return CGSizeZero;
}
}
#end
Unable to figure out why cell size is not as expected.

ios - horizontally scrolling collectionView with images

I am trying to show images on collectionView. I have trouble showing this images as I have created a reusable cell with imageView inside.Those images have to be equally spaced between as I want to show 3 icons on screen at once. I am using 13 icons and it has to be scrollable horizontally through screen.
I am not able to show image in cell and I do not know how to set image cells with spacing between them using just one reusable cell
CustomCollectionViewCell.h
#interface CustomCollectionViewCell : UICollectionViewCell
#property (nonatomic, retain) UIImageView *imageView;
#end
CustomCollectionViewCell.m
#implementation CustomCollectionViewCell
- (UIImageView *) imageView {
if (!_imageView) {
_imageView = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
[self.contentView addSubview:_imageView];
}
return _imageView;
}
- (void)prepareForReuse {
[super prepareForReuse];
[self.imageView removeFromSuperview];
self.imageView = nil;
}
#end
LandingViewController.m
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
CustomCollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:#"CustomCell" forIndexPath:indexPath];
return cell;
}
Now OP wants horizontal scrollable direction of Collection view.So Again I created small sample project for this.I put 13 images in horizontal scroll direction.It scrolls successfully on horizontal direction of collection view.
Why I post the horizontal scrollable collectionView here is everyone can understand the answer and get the solution easily.I added extra image I mean exactly 13 images(op wants 13 images into collection view). This answer definitely helps you.
HorizontalScrollableCollectionView
Here I set the scrollDirection as Horizontal in CustomImageFlowLayout.m file
CustomImageFlowLayout.h
#import <UIKit/UIKit.h>
#interface CustomImageFlowLayout : UICollectionViewFlowLayout
#end
CustomImageFlowLayout.m
#import "CustomImageFlowLayout.h"
#implementation CustomImageFlowLayout
- (instancetype)init
{
self = [super init];
if (self)
{
self.minimumLineSpacing = 1.0f;
self.minimumInteritemSpacing = 1.0f;
self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
}
return self;
}
- (CGSize)itemSize
{
NSInteger numberOfColumns;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
numberOfColumns = 3;
else{
if([UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortrait)
numberOfColumns = 4;
else if([UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationLandscapeRight || [UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationLandscapeLeft)
numberOfColumns = 4;
}
NSLog(#"The collection view frame is - %#",NSStringFromCGRect(self.collectionView.frame));
CGFloat itemWidth = (CGRectGetWidth(self.collectionView.frame) - (numberOfColumns - 1)) / numberOfColumns;
NSLog(#"The item width is - %f",itemWidth);
return CGSizeMake(itemWidth, itemWidth);
}
#end
Then CustomCell of UICollectionViewCell
CustomCell.xib
CustomCell.h
#import <UIKit/UIKit.h>
#interface CustomCell : UICollectionViewCell
#property (strong, nonatomic) IBOutlet UIImageView *img;
#property (strong, nonatomic) IBOutlet UILabel *lblCollection;
#end
CustomCell.m
#import "CustomCell.h"
#implementation CustomCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
#end
Now Storyboard design starts
My collection view name is here collectionviewVerticalHorizontalFlowLayout
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UICollectionViewDelegate,UICollectionViewDataSource>
#property (strong, nonatomic) IBOutlet UICollectionView *collectionviewVerticalHorizontalFlowLayout;
#end
ViewController.m
#import "ViewController.h"
#import "CustomCell.h"
#import "CustomImageFlowLayout.h"
#interface ViewController (){
NSMutableArray *arrayImages;
NSMutableArray *arrayTitles;
CustomCell *cell;
}
#end
#implementation ViewController
#synthesize collectionviewVerticalHorizontalFlowLayout;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
collectionviewVerticalHorizontalFlowLayout.collectionViewLayout = [[CustomImageFlowLayout alloc] init];
collectionviewVerticalHorizontalFlowLayout.backgroundColor = [UIColor clearColor];
arrayImages = [[NSMutableArray alloc]initWithObjects:#"iPhone.png", #"android.png", #"windows.png", #"blackberry.png", #"lenovovibek5note.png", #"redmi.png", #"moto.png", #"sony.png", #"samsung.png", #"oneplus.png",#"redminote4.png",#"oppo.png",#"vivo.png",nil];
arrayTitles = [[NSMutableArray alloc]initWithObjects:#"iPhone", #"Android", #"Windows", #"Blackberry", #"LenovaVikeK5Note", #"Redmi", #"MotoG", #"Sony", #"Samsung", #"OnePlus", #"RedMiNote4",#"Oppo",#"Vivo",nil];
UINib *cellNib = [UINib nibWithNibName:#"CustomCell" bundle:nil];
[collectionviewVerticalHorizontalFlowLayout registerNib:cellNib forCellWithReuseIdentifier:#"cell"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//UICollectionView Data Source Methods
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return arrayImages.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"cell";
cell = (CustomCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
if(cell==nil){
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = nib[0];
}
cell.img.image = [UIImage imageNamed:(NSString*)[arrayImages objectAtIndex:indexPath.row]];
NSLog(#"The collection view label text is - %#",[NSString stringWithFormat:#"%#",arrayTitles[indexPath.row]]);
cell.lblCollection.text = arrayTitles[indexPath.row];
cell.lblCollection.textColor = [UIColor blackColor];
return cell;
}
//UICollectionView Delegate Method
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"the clicked indexPath.row is - %ld",(long)indexPath.row);
}
#end
Final out put screen shots
First it shows
Then If I scroll horizontally in the collection view it shows
I will give you what you ask.
I wanted to same thing in one application.I successfully implemented this.Just now I tried the sample project for your question.I got what you ask exactly.I show you the code and everything below.
First CustomImageFlowLayout of NSObject Class
CustomImageFlowLayout.h
#import <UIKit/UIKit.h>
#interface CustomImageFlowLayout : UICollectionViewFlowLayout
#end
CustomImageFlowLayout.m
#import "CustomImageFlowLayout.h"
#implementation CustomImageFlowLayout
- (instancetype)init
{
self = [super init];
if (self)
{
self.minimumLineSpacing = 1.0f;
self.minimumInteritemSpacing = 1.0f;
self.scrollDirection = UICollectionViewScrollDirectionVertical;
}
return self;
}
- (CGSize)itemSize
{
NSInteger numberOfColumns;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
numberOfColumns = 3;
else{
if([UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortrait)
numberOfColumns = 4;
else if([UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationLandscapeRight || [UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationLandscapeLeft)
numberOfColumns = 4;
}
NSLog(#"The collection view frame is - %#",NSStringFromCGRect(self.collectionView.frame));
CGFloat itemWidth = (CGRectGetWidth(self.collectionView.frame) - (numberOfColumns - 1)) / numberOfColumns;
NSLog(#"The item width is - %f",itemWidth);
return CGSizeMake(itemWidth, itemWidth);
}
#end
After that I created Custom Cell for Images
See my Design first
CustomCell.h
#import <UIKit/UIKit.h>
#interface CustomCell : UICollectionViewCell
#property (strong, nonatomic) IBOutlet UIImageView *img;
#property (strong, nonatomic) IBOutlet UILabel *lblCollection;
#end
CustomCell.m
#import "CustomCell.h"
#implementation CustomCell
#end
Then I use above class in my ViewController
Below is my Storyboard Design
Here my CollectionView name is collectionViewVertical
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
#property (strong, nonatomic) IBOutlet UICollectionView *collectionViewVertical;
#end
ViewController.m
#import "ViewController.h"
#import "CustomCell.h"
#import "CustomImageFlowLayout.h"
#interface ViewController ()
{
NSMutableArray *arrayImages;
NSMutableArray *arrayTitles;
CustomCell *cell;
}
#end
#implementation ViewController
#synthesize collectionViewVertical;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
collectionViewVertical.collectionViewLayout = [[CustomImageFlowLayout alloc] init];
collectionViewVertical.backgroundColor = [UIColor clearColor];
arrayImages = [[NSMutableArray alloc]initWithObjects:#"iPhone", #"Android", #"Windows", #"Blackberry", #"Lenova", #"Redmi", #"MotoG", #"Sony", #"Samsung", #"OnePlus", nil];
arrayTitles = [[NSMutableArray alloc]initWithObjects:#"iPhone.png", #"android.png", #"windows.png", #"blackberry.png", #"lenovo.png", #"redmi.png", #"moto.png", #"sony.png", #"samsung.png", #"oneplus.png", nil];
UINib *cellNib = [UINib nibWithNibName:#"CustomCell" bundle:nil];
[collectionViewVertical registerNib:cellNib forCellWithReuseIdentifier:#"cell"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//UICollectionView Data Source Methods
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return arrayImages.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"cell";
cell = (CustomCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
if(cell==nil){
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = nib[0];
}
cell.img.image = [UIImage imageNamed:(NSString*)[arrayImages objectAtIndex:indexPath.row]];
cell.lblCollection.text = arrayTitles[indexPath.row];
return cell;
}
//UICollectionView Delegate Method
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"the clicked indexPath.row is - %ld",(long)indexPath.row);
}
Finally the output screen

IOS UICollectionView Cell with different instance of cell class

I am trying to create custom cell for UiCollectionView where I need to update each cell from background.
So I have created custom class for each cell named Cell_Obj and updating the cell content from the Cell_Obj itself using a timer.
The below code add an image on cell and increment a counter in each 2 second and display it on new cell label.
On each time when I add new cell using a button on a viewcotroller the cell updating but every cell getting the same value on the label. It suppose to have different counter value as each cell is different instance of Cell_Obj but the counter value and labelTxt(the cell number) has the same value when each time the timer triggered.
ViewController.h
#import <UIKit/UIKit.h>
#import "Cell_Obj.h"
#interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>
#property (weak, nonatomic) IBOutlet UICollectionView *collection;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController (){
NSMutableArray *GridArray;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.collection setDelegate:self];
[self.collection setDataSource:self];
// Do any additional setup after loading the view, typically from a nib.
GridArray = [[NSMutableArray alloc] init];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return GridArray.count;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (Cell_Obj *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
Cell_Obj *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
// cell.label.text = #"123";
/*cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:#"AppIcon.png",indexPath.row]];*/
return cell;
}
- (void)addImage
{
Cell_Obj *cell = [[Cell_Obj alloc] init];
// cell.label.text = #"123";
//cell.label.text = [NSString stringWithFormat:#"%d",[dvrGridArray count]-1];
NSString * txt = [NSString stringWithFormat:#"%d",[GridArray count]];
[cell updateTextLabelName: txt];
[GridArray addObject:cell];
[_collection insertItemsAtIndexPaths:#[[NSIndexPath indexPathForItem:[GridArray count]-1 inSection:0]]];
/* NSMutableArray *arrayWithIndexPaths = [NSMutableArray array];
[arrayWithIndexPaths addObject:cell];
[self.collection insertItemsAtIndexPaths:arrayWithIndexPaths];*/
}
- (IBAction)addClicked:(id)sender {
[self addImage];
}
- (IBAction)changeImage:(id)sender {
// DVR_Obj *cell = [dvrGridArray objectAtIndex:0];
// [cell changeImage ];
// [_collection reloadData];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
Cell_Obj *cell = [_collection cellForItemAtIndexPath:indexPath];
[cell changeImage ];
}
#pragma mark Collection view layout things
// Layout: Set cell size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
//NSLog(#"SETTING SIZE FOR ITEM AT INDEX %d", indexPath.row);
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
CGSize mElementSize = CGSizeMake((screenWidth/4)-2, (screenHeight/4)-2);
return mElementSize;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 1.0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 1.0;
}
// Layout: Set Edges
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
// return UIEdgeInsetsMake(0,8,0,8); // top, left, bottom, right
return UIEdgeInsetsMake(1,1,1,1); // top, left, bottom, right
}
#end
Cell_Obj.h
#import <UIKit/UIKit.h>
#interface Cell_Obj : UICollectionViewCell
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
#property (weak, nonatomic) IBOutlet UILabel *label;
- (void)changeImage;
- (void)updateTextLabelName:(NSString*)str;
#end
Cell_Obj.m
#import "Cell_Obj.h"
static NSString *labelTxt ;// = [[NSString alloc] init];
static int counter;
#implementation Cell_Obj{
}
+ (void)initialize {
if (self == [Cell_Obj class]) {
labelTxt = [[NSString alloc] init];
counter=0;
}
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)awakeFromNib {
_imageView.image = [UIImage imageNamed:#"flower1.png"];
_label.text = labelTxt;
[NSTimer scheduledTimerWithTimeInterval:2.0f
target:self
selector:#selector(updateImage)
userInfo:nil
repeats:YES];
}
- (void)updateImage
{
_imageView.image = [UIImage imageNamed:#"AppIcon.png"];
counter++;
NSString * txt = [NSString stringWithFormat:#"%#-%d",labelTxt,counter];
_label.text = txt;
}
- (void)updateTextLabelName :(NSString*)str
{
labelTxt = str;
}
#end
Your each cell is not new cell. Cell_Obj *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath]; this statement reuses your cell with identifier "Cell". To create new cell each time , don't pass indexPath, pass nil instead.

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

Resources