0

I am trying to use a collection view and reuse a collection view cell and I have to get 23 images and name from server which I have not started coz of the problem I am facing

This is my custom cell file collection view file objective c

Below is my custom cell.h

#import <UIKit/UIKit.h>

@interface CustomCell : UICollectionViewCell

{
    UIImageView *imageView;
}

@property (nonatomic, retain) UIImageView *imageView; //this imageview   is the only thing we need right now.


@end

Here is my custom cell.m file

#import "CustomCell.h"

@implementation CustomCell

@synthesize imageView;

- (id)initWithFrame:(CGRect)aRect
{
    if (self = [super initWithFrame:aRect])
    {
    //we create the UIImageView in this overwritten init so that we always have it at hand.
    imageView = [[UIImageView alloc] init];
    //set specs and special wants for the imageView here.
    [self addSubview:imageView]; //the only place we want to do this addSubview: is here!

    //You wanted the imageView to react to touches and gestures. We can do that here too.
    UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onButtonTapped:)];
    [tap setNumberOfTapsRequired:1];
    [self addGestureRecognizer:tap];


    //We can also prepare views with additional contents here!
    //just add more labels/views/whatever you want.
    }
    return self;
    }

    -(void)onButtonTapped:(id)sender
    {
     //the response to the gesture.
     //mind that this is done in the cell. If you don't want things to happen from this cell.
     //then you can still activate this the way you did in your question.  
     }
 @end

These are the methods in my normal view controller.m file

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cvCell" forIndexPath:indexPath];

    [[[cell contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];


    // create a uiview where we can place all views that need to go into this cell
    UIView * contents=[[UIView alloc]      initWithFrame:cell.contentView.bounds];
    [contents setBackgroundColor:[UIColor clearColor]];
    [cell.contentView addSubview:contents];


    // set tag to the indexPath.row so we can access it later
    [cell setTag:indexPath.row];

    // add interactivity
    UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc]  initWithTarget:self action:@selector(onButtonTapped:)];
    [tap setNumberOfTapsRequired:1];
    [cell addGestureRecognizer:tap];


if (cell.selected) {
    cell.backgroundColor = [UIColor blueColor]; // highlight selection
}
else
{
    cell.backgroundColor = [UIColor redColor]; // Default color
}
return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath  {

    UICollectionViewCell *datasetCell =[collectionView cellForItemAtIndexPath:indexPath];
    datasetCell.backgroundColor = [UIColor blueColor]; // highlight selection
}

-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {

UICollectionViewCell *datasetCell =[collectionView cellForItemAtIndexPath:indexPath];
datasetCell.backgroundColor = [UIColor redColor]; // Default color
}

I don't get any cell when I moved to this view, it is just blank with the background image and I have even given a blank picture to see how the view is but I don't get anything please help!!!

Mr.Kushwaha
  • 491
  • 5
  • 14
Akshay
  • 179
  • 1
  • 16
  • 1
    Have you set the collection view's delegate and datasource? – Aravind A R Jun 12 '17 at 06:38
  • @Akshay :- Have you registered custom cell ??.. – Developer Jun 12 '17 at 06:40
  • @coder how to register custom celll?? – Akshay Jun 12 '17 at 06:42
  • @AravindAR it shud be given to the view controller where I use collection view rite?? – Akshay Jun 12 '17 at 06:42
  • yes I did it and get red cells thank u!! – Akshay Jun 12 '17 at 06:43
  • but I dont see a image view or uilabel which I had in the cell – Akshay Jun 12 '17 at 06:43
  • Add this line of code in viewDidLoad [collectionView registerClass:[CustomViewCell class] forCellWithReuseIdentifier:@"CustomIdentifier"]; and CustomViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CustomIdentifier" forIndexPath:indexPath]; this in cellForItemAtindexPath – Developer Jun 12 '17 at 06:44
  • @coder I am getting error no known method in the current class visible – Akshay Jun 12 '17 at 06:46
  • I don't have a cellforrowatindexpath – Akshay Jun 12 '17 at 06:47
  • - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath Replace the blow line of code UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cvCell" forIndexPath:indexPath]; – Developer Jun 12 '17 at 06:48
  • @Akshay is it working now ? – Aravind A R Jun 12 '17 at 06:52
  • [UICollectionView registerClass:[CustomCell class] forCellWithReuseIdentifier:@"CustomIdentifier"]; this line is giving me error when I used it in view did oad – Akshay Jun 12 '17 at 06:52
  • @Akshay Are you adding cells to the collection view from xib or are you using storyboard ? – Aravind A R Jun 12 '17 at 06:58
  • @AravindAR I am using storyboard – Akshay Jun 12 '17 at 06:58
  • Then you don't need to use UICollectionView registerClass. You are now able to see the cells right, only thing missing is imageview ? Are you giving color to imageview or are you setting any default image to image view? – Aravind A R Jun 12 '17 at 07:01
  • @AravindAR yes I have given a default picture to image view – Akshay Jun 12 '17 at 07:02
  • @AravindAR and a label text is default as label – Akshay Jun 12 '17 at 07:05
  • @Akshay were are you giving the default image?? – Aravind A R Jun 12 '17 at 07:05
  • @AravindAR I have given it in story board to view the cells – Akshay Jun 12 '17 at 07:05
  • Then you don't have to add imageView = [[UIImageView alloc] init]; [self addSubview:imageView]; [[[cell contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)]; // create a uiview where we can place all views that need to go into this cell UIView * contents=[[UIView alloc] initWithFrame:cell.contentView.bounds]; [contents setBackgroundColor:[UIColor clearColor]]; [cell.contentView addSubview:contents]; – Aravind A R Jun 12 '17 at 07:07
  • @AravindAR I shud add this in uicollectionviewcell method rite?? – Akshay Jun 12 '17 at 07:09
  • I am getting use of undeclared identifier error for imageView – Akshay Jun 12 '17 at 07:12
  • @Akshay Since there are few changes I have mentioned all the changes in the answer that I have added. – Aravind A R Jun 12 '17 at 07:24

1 Answers1

1

The cells are not displaying because you might not have set the datasource and delegate properly. Since you have added the cells in storyboard you don't need to add the imageview as subview in the collection view cell. First connect the outlet of imageview that you have added in storyboard to the CustomCell. Then remove

{
    UIImageView *imageView;
}

@property (nonatomic, retain) UIImageView *imageView;

You can remove - (id)initWithFrame:(CGRect)aRect this method and you can add the tap gesture initialization in awakeFromNib method

-(void)awakeFromNib {
  UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onButtonTapped:)];
    [tap setNumberOfTapsRequired:1];
    [self addGestureRecognizer:tap];
}

No need to do

 imageView = [[UIImageView alloc] init];
    [self addSubview:imageView]; 

Again in cellForItemAtIndexPath remove the following lines

[[[cell contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
UIView * contents=[[UIView alloc]      initWithFrame:cell.contentView.bounds];
[contents setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:contents];

After making these changes please try again

Aravind A R
  • 2,674
  • 1
  • 15
  • 25