Miki
Miki

Reputation: 921

How to instantiate custom class UICollectionViewCell outside CollectionView delegate methods?

I have trouble instantiating custom class UICollectionViewCell in my custom method. I already have desired NSIndexPath that i need, i only need to instantiate that one cell so i can put some progress view in it...

Here is my example code:

-(void)setupProgressAtIndexPath:(NSIndexPath *)indexPath {

    StoreViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    _progressBackground = [[UIView alloc] initWithFrame:CGRectMake(cell.frame.size.width/6,cell.frame.size.height/6,80,80)];
    else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        _progressBackground = [[UIView alloc] initWithFrame:CGRectMake(cell.frame.size.width/4,cell.frame.size.height/4,80,80)];

    _progressBackground.alpha = 0.95;
    _progressBackground.backgroundColor=[UIColor whiteColor];
    _progressBackground.layer.cornerRadius = 20.0f;
    _progressBackground.hidden=NO;

    _progressView = [[M13ProgressViewPie alloc] init];
    _progressView.backgroundRingWidth=2.0;
    _progressView.frame = CGRectMake(0,0,64,64);
    _progressView.clipsToBounds=YES;
    _progressView.center = CGPointMake(40,40);
    _progressView.primaryColor=[UIColor orangeColor];
    _progressView.secondaryColor=[UIColor orangeColor];

    [_progressBackground setHidden:YES];

    [_progressBackground addSubview:_progressView];
    [cell.magazineImage addSubview:_progressBackground];

}

Ok i call this in delegate method -collectionView: didSelectItemAtIndexPath:

There is only one problem, when i tap on some cell, it puts progress view there but cell loses it's data and becomes nil. Other that that all works fine. I think the only problem is this line of code:

StoreViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

Is there any other resolution to my problem, is there any other way to instantiate cell without losing data, i need some answers! :)

Upvotes: 0

Views: 1072

Answers (3)

Vipul
Vipul

Reputation: 111

StoreViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];

Explanation:

- cellForItemAtIndexPath:indexPath returns nil if cell is not visible or index path is out of range. This doesn't create cells, only gives you access to them. I think it should be avoided as much as possible to prevent accidental leaks and other interference with the tableView.

Upvotes: 2

metronic
metronic

Reputation: 455

Instead of this:

StoreViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

extract the existing cell at that index:

StoreViewCell * cell = [self.collectionView cellForItemAtIndexPath:indexPath];

Even if it might work I suggest you to put _progressBackground and _progressView related stuff inside the StoreViewCell class as hidden and then unhide them only when you need to

Upvotes: 2

Wain
Wain

Reputation: 119021

Replace

StoreViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

with

 StoreViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];

to get the existing cell to update - you shouldn't be creating a new cell.

Upvotes: 1

Related Questions