UICollectionViewCell 是否有 viewWillAppear 或我可以将数据传递给的东西

cdu*_*dub 3 objective-c uiviewcontroller ios uicollectionview uicollectionviewcell

我的视图控制器中有这个:

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

     cell.dataObjects = data;

     return cell;
}
Run Code Online (Sandbox Code Playgroud)

在我的 CellSubclass.m 中,我有:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

所以在我的视图控制器的生命周期中, CellSubclass 在数据准备好之前立即被调用。有没有像 viewWillAppear 这样的函数或者我也可以传递数据的东西?除了 initWithFrame 之外,如何从我的视图控制器中调用函数?我可以通过其他方式传递数据吗?

谢谢。

小智 6

UICollectionViewCell没有类似的东西,但它是 UIView 的一个子类,并且有didMoveToSuperview。我在某些特定情况下在生产中使用它,但无论如何它也有助于一些快速调试。

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


小智 5

在 cellForItemAtIndexPath 中为单元格使用自己的配置方法听起来更好。

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

     cell.dataObjects = fetchedMUtableArray[indexPath.row];

     [cell somethingConfiguringMethods];

     return cell;
}
Run Code Online (Sandbox Code Playgroud)