Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UICollectionViewCell是否有一个ViewWillDisplay或我可以向其传递数据的对象_Ios_Objective C_Uiviewcontroller_Uicollectionview_Uicollectionviewcell - Fatal编程技术网

Ios UICollectionViewCell是否有一个ViewWillDisplay或我可以向其传递数据的对象

Ios UICollectionViewCell是否有一个ViewWillDisplay或我可以向其传递数据的对象,ios,objective-c,uiviewcontroller,uicollectionview,uicollectionviewcell,Ios,Objective C,Uiviewcontroller,Uicollectionview,Uicollectionviewcell,我的视图控制器中有: - (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { CellSubclass *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:

我的视图控制器中有:

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

     cell.dataObjects = data;

     return cell;
}
在我的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;
}
因此,在我的视图控制器的生命周期中,CellSubclass会在数据准备就绪之前立即被调用。是否有类似ViewWillDisplay之类的函数,或者我也可以传递数据的函数?除了initWithFrame之外,如何从视图控制器调用函数?我可以用其他方式传递数据吗


谢谢。

如果没有任何数据可显示,则应将单元格数返回为
0
。数据准备就绪后,只需重新加载集合视图

-(void)fetchData {
    [httpClient fetchDataWithCompletionBlock:^(NSArray *data) {
        [self.collectionView reloadData];
    }];
}
在您的数据源方法中

- (NSInteger)collectionView:(UICollectionView *)collectionView
     numberOfItemsInSection:(NSInteger)section {
    if (self.collectionViewData)
        return self.collectionViewData.count; 
    else
        return 0;
}
假设您有一个异步网络调用,它返回了数据。 在完成块中,只需重新加载集合视图

-(void)fetchData {
    [httpClient fetchDataWithCompletionBlock:^(NSArray *data) {
        [self.collectionView reloadData];
    }];
}

对于cellForItemAtIndexPath中的单元格,最好使用自己的配置方法

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

     cell.dataObjects = fetchedMUtableArray[indexPath.row];

     [cell somethingConfiguringMethods];

     return cell;
}

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


在完成块中使用reloadCollectionView方法听起来更好,该方法在数据获取完成时调用。在:-(NSInteger)collectionView:(UICollectionView*)collectionView numberofitemsinssection:(NSInteger)中是哪个函数section@chris这就是你的数据源方法。如何将数据传递给CellSubclass并在InitWithFrame中使用我有一个parse PFQuery,它在我的视图控制器viewDidLoad和ViewDidScroll中加载NSMutableArray不知道如何将数据从我的视图控制器中的NSMutableArray获取到子类UICollectionViewCell中,该子类UICollectionViewCell使用其initWithFrameI中的数据[cell somthingConfiguringMethods]和somthingConfiguringMethods在cell子类中是否正确?哦,对不起,是的。somthingConfiguringMethods在cell实现文件中定义。这正是我需要的,以便在加载视图后允许我进行一些约束修改。谢谢@Boris