0

I'm subclassing my UICollectionViewController cell like this :

-(CategoryScreenCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"cellForItemAtIndexPath load");
    CategoryScreenCell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];

the cell Identifier is set to "MyCell" and custome cell class to

"CategoryScreenCell". the "CategoryScreenCell" Class:

@implementation CategoryScreenCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    NSLog(@"CategoryScreenCell before init");
    if (self) {
        NSLog(@"CategoryScreenCell inside init");
        // Initialization code
        _imageView = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
        [self.contentView addSubview:_imageView];
    }
    return self;
}

I'm using storyboard and both delegate and source of my UICollevtionViewController

are set to own class.

this is the method of loading data into my UICollectionView :

parser.completionBlock = ^(void) {
    if (weakParser.appRecordList) {
        dispatch_async(dispatch_get_main_queue(), ^{
            UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                                     bundle: nil];
            MyCollectionViewController *svc =[mainStoryboard instantiateViewControllerWithIdentifier:@"MainCategory"];
            svc.entries = weakParser.appRecordList;

            NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
            NSIndexPath *indexPath;
            for (int i = 0; i < [weakParser.appRecordList count]; i++) {
                indexPath = [NSIndexPath indexPathForItem:i inSection:0];
                [indexPaths addObject:indexPath];
            }

            [svc.collectionView reloadItemsAtIndexPaths:indexPaths];

        });

    }

    self.queue = nil;
};

im using custome layout for my collection view (RFQuiltLayout).

im setting this layout in my storyboard to collectionviewcontroller so it gets

inits with load.

this my MyCollectionViewController code :

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
    }
    return self;
}

    - (void)viewDidLoad
    {

        [super viewDidLoad];
        self.automaticallyAdjustsScrollViewInsets = NO;
       NSLog(@"inside  ");
       NSLog(@"self.entries : %@  ",self.entries);
        UIButton *button_right =  [UIButton buttonWithType:UIButtonTypeCustom];
        [button_right setImage:[UIImage imageNamed:@"setting_btn.png"] forState:UIControlStateNormal];
        [button_right setFrame:CGRectMake(0, 0, 30, 30)];
        SWRevealViewController *revealViewController = self.revealViewController;
        if ( revealViewController )
        {
            [button_right addTarget:self.revealViewController action:@selector(revealToggle:) forControlEvents:UIControlEventTouchUpInside];
            [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
        }
    }

for test i put nslog on most places (blockSizeForItemAtIndexPath , insetsForItemAtIndexPath , numberOfItemsInSection) and they all returns values

this is the log file :

2015-03-07 11:52:46.001 News[9464:919944] numberOfSectionsInCollectionView :1
2015-03-07 11:52:46.001 News[9464:919944] numberOfItemsInSection : 10  
2015-03-07 11:52:46.002 News[9464:919944] blockSizeForItemAtIndexPath
2015-03-07 11:52:46.005 News[9464:919944] insetsForItemAtIndexPath
2015-03-07 11:52:46.005 News[9464:919944] blockSizeForItemAtIndexPath
2015-03-07 11:52:46.006 News[9464:919944] insetsForItemAtIndexPath
2015-03-07 11:52:46.006 News[9464:919944] blockSizeForItemAtIndexPath
2015-03-07 11:52:46.006 News[9464:919944] insetsForItemAtIndexPath
2015-03-07 11:52:46.009 News[9464:919944] blockSizeForItemAtIndexPath
2015-03-07 11:52:46.009 News[9464:919944] insetsForItemAtIndexPath
2015-03-07 11:52:46.009 News[9464:919944] blockSizeForItemAtIndexPath
2015-03-07 11:52:46.010 News[9464:919944] insetsForItemAtIndexPath
2015-03-07 11:52:46.010 News[9464:919944] blockSizeForItemAtIndexPath
2015-03-07 11:52:46.010 News[9464:919944] numberOfSectionsInCollectionView load

as logs shows out "CategoryScreenCell" not getting called.

this is my whole cellForItemAtIndexPath method :

-(CategoryScreenCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"cellForItemAtIndexPath load");
    CategoryScreenCell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
    UILabel *label = (UILabel *)[myCell.contentView viewWithTag:10];
    UILabel *catlbl = (UILabel *)[myCell.contentView viewWithTag:11];
    UILabel *bglbl = (UILabel *)[myCell.contentView viewWithTag:12];
    NSUInteger nodeCount = [self.entries count];

    if (nodeCount == 0)
    {

    myCell.imageView.image  = [UIImage imageNamed:@"logo.png"];

         myCell.imageView.hidden = YES;
         label.hidden = YES;
         catlbl.hidden = YES;
    }
    if (nodeCount > 0)
    {
        catlbl.hidden=YES;
        AppRecord *appRecord = [self.entries objectAtIndex:indexPath.item];
        if(appRecord.title != NULL)
        {
        label.font = [UIFont fontWithName:@"B Yekan" size:12];
           [label setText:appRecord.title];
            NSLog(@"appRecord.title %@",appRecord.title);
            [bglbl setText:@""];
            [bglbl setBackgroundColor:[UIColor clearColor]];
            CAGradientLayer *gradientLayer = [CAGradientLayer layer];
            gradientLayer.frame = bglbl.bounds;
            gradientLayer.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:176.0f green:176.0f blue:176.0f alpha:0]CGColor], (id)[[UIColor colorWithRed:0 green:0 blue:0 alpha:1]CGColor], nil];
            [bglbl.layer insertSublayer:gradientLayer atIndex:0];
        }
        NSString *id_tring= [NSString stringWithFormat:@"%@",appRecord.ids];
        [catlbl setText:id_tring];


        if (![appRecord.thumb_url isEqualToString:@""])
        {
            __block UIActivityIndicatorView *activityIndicator;
            __weak UIImageView *weakImageView = myCell.imageView;
            [myCell.imageView sd_setImageWithURL:[NSURL URLWithString:appRecord.thumb_url]
                              placeholderImage:nil
                                       options:SDWebImageProgressiveDownload
                                      progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                                          if (!activityIndicator) {
                                              [weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]];
                                              activityIndicator.center = weakImageView.center;
                                              [activityIndicator startAnimating];
                                          }
                                      }
                                     completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                                         [activityIndicator removeFromSuperview];
                                         activityIndicator = nil;

                                     }];

        }
    }
    myCell.clipsToBounds = YES;
    myCell.layer.masksToBounds = YES;
    myCell.layer.cornerRadius = 6;
    return myCell;
}

How can I resolve this ?

Code cracker
  • 3,105
  • 6
  • 37
  • 67
Ahad Porkar
  • 1,666
  • 2
  • 33
  • 68
  • 1
    try to put your code of custom Collection View Cell in -(id)initWithCoder:(NSCoder *)aDecoder method instaed of -initwithFrame – SandeepAggarwal Mar 07 '15 at 09:06
  • can you please elaborate or explain how can i achieve this ? what about initWithFrame:frame ? should i use "initWithCoder:aDecoder" instead ? ive tried but still nothing apeard.(one thing is now class getting called) im updating my question due to ur suggestion. – Ahad Porkar Mar 07 '15 at 09:11

2 Answers2

1
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonMethod ];
    }

    return self;
}


-(id)initWithCoder:(NSCoder *)aDecoder

{
   self=[super initWithCoder:aDecoder];

    if (self) {
       [self commonMethod ];
    }
   return self;


}


-(void)commonMethod
{
NSLog(@"CategoryScreenCell before init");
    if (self) {
        NSLog(@"CategoryScreenCell inside init");
    // Initialization code
        _imageView = [[UIImageView alloc]   initWithFrame:self.contentView.bounds];
        [self.contentView addSubview:_imageView];
_imageView.tag=8;
 _label.tag=9;
 _catlbl.tag=10;
 //SO ON..
    }

}

//lets say you have give tag number as 8 to imageView in 'commonMethod'

 -(CategoryScreenCell *)collectionView:(UICollectionView   *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"cellForItemAtIndexPath load");
    CategoryScreenCell *myCell = [collectionView     dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
UIImageView *imageView = (UIImageView *)[cell viewWithTag:8];
[imageView setImage : @"logo.png"];
[imageview setNeedsLayout];

//comment out the rest of code and try this and check whether image is     seen in the cells

}

Also set the class of UICollectionViewCell in storyboard as 'yourCustomCollectionViewCell' class

SandeepAggarwal
  • 1,273
  • 3
  • 14
  • 38
  • thanks for suggestion. ive tried ur code and nslog shows the method is calling but still no cell shows up in uicollectionview controller. – Ahad Porkar Mar 07 '15 at 09:16
  • is this NSLog working NSLog(@"cellForItemAtIndexPath load"); ? – SandeepAggarwal Mar 07 '15 at 09:19
  • yes i have "cellForItemAtIndexPath load" in nsloag. it seems everything works well except the cell not drawing on uicollectionviewcontroller. – Ahad Porkar Mar 07 '15 at 09:23
  • show the full code for "cellForItemAtIndexPath " – SandeepAggarwal Mar 07 '15 at 09:24
  • you have not unhide the labels and imageView after updating them. – SandeepAggarwal Mar 07 '15 at 09:36
  • i removed all hidden part and nothing happend. – Ahad Porkar Mar 07 '15 at 09:40
  • Have you unhidden them in cellforRowAtIndexPath method . And are you correctly using the tag numbers 10, 11 and 12, i mean are you sure the same tag numbers you have assigned them in custom Collection View class in the code itself. – SandeepAggarwal Mar 07 '15 at 09:43
  • yes i double checked the tag and they are same. one thing i should mention that in my old code everthing works well before moditification. this my modification thread : http://stackoverflow.com/questions/28903541/topviewcontroller-unrecognized-selector-error-with-custome-segue-to-navigationc – Ahad Porkar Mar 07 '15 at 09:47
  • try to NSLog whether are you able to go inside the block if (nodeCount > 0) { }, I doubt the self.entries property is not being updated correctly. – SandeepAggarwal Mar 07 '15 at 09:51
  • the "NSLog(@"appRecord.title %@",appRecord.title);" shows my value correctly. – Ahad Porkar Mar 07 '15 at 09:53
  • try one thing after setting image in cellForRowAtIndexPath : [cell setNeedsLayout]; – SandeepAggarwal Mar 07 '15 at 10:04
  • ive set [myCell setNeedsLayout]; this in end of view but no hope :( i guess im cursed with this modification. – Ahad Porkar Mar 07 '15 at 10:08
  • i removed the code entirely and replaced ur suggested code but still nothing appeard. fun part is even if remove the content of cell and make cell color the black nothing shows up. – Ahad Porkar Mar 07 '15 at 10:24
0

You need to register your custom cell class to your collectionview

[_collectionView registerClass:[CategoryScreenCell class] forCellWithReuseIdentifier:@"MyCell"];

After registration of class than only you can get its instances and memory

haresh
  • 486
  • 3
  • 18