Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does UICollectionViewCell have a viewWillAppear or something I can pass data to

I have this in my view controller:

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

     cell.dataObjects = data;

     return cell;
}

In my CellSubclass.m I have:

- (id) initWithFrame:(CGRect)frame
{
    // You can call initWithFrame: method here as our base constructor of the UIView super class
    self = [super initWithFrame:frame];

    if (self)
    {
        // call functions that need access to data but data is nil
    }

    return self;
}

So in the lifecycle of my view controller, the CellSubclass gets called immediately before the data is ready. Is there a function like viewWillAppear or something that I can pass the data too? How can I call a function from my view controller other than initWithFrame? Can I pass the data in other ways?

Thanks.

like image 895
cdub Avatar asked Nov 14 '25 20:11

cdub


2 Answers

It sounds better to use your own configure method for cell in cellForItemAtIndexPath.

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

     cell.dataObjects = fetchedMUtableArray[indexPath.row];

     [cell somethingConfiguringMethods];

     return cell;
}
like image 168
merge Avatar answered Nov 17 '25 08:11

merge


UICollectionViewCell does not have something like that, but that is a subclass of UIView and that one has didMoveToSuperview. I use it in production for some specific cases, but anyway it helps for some fast debugging also.

https://developer.apple.com/documentation/uikit/uiview/1622433-didmovetosuperview

like image 21
Boris Avatar answered Nov 17 '25 09:11

Boris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!