Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.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中的子视图控制器_Ios_Objective C_Uicollectionview_Childviewcontroller - Fatal编程技术网

Ios UICollectionViewCell中的子视图控制器

Ios UICollectionViewCell中的子视图控制器,ios,objective-c,uicollectionview,childviewcontroller,Ios,Objective C,Uicollectionview,Childviewcontroller,假设我想实现Pinterest的pin页面,如下所示: 这是我的方法: 制作一个UICollectionViewController,pin的页面是一个UICollectionViewCell 单元格由两个组件组成:pin信息子vc和瀑布子vc 接下来的问题是:如何重用子视图控制器 一些伪代码: - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(

假设我想实现Pinterest的pin页面,如下所示:

这是我的方法:

  • 制作一个
    UICollectionViewController
    ,pin的页面是一个
    UICollectionViewCell
  • 单元格由两个组件组成:pin信息子vc和瀑布子vc
  • 接下来的问题是:如何重用子视图控制器

    一些伪代码:

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
        Pin *pin = self.dataSource[indexPath.row];
        // i have to create a new childVC, based on different indexPath.
        UITableViewController *pinInfoViewController = [[pinInfoViewController alloc] initWithPin:pin];
        [cell updatePinViewWithPin:pin];
        [self addChildViewController:pinInfoViewController];
    
        // Add waterfall view controller
    }
    

    每次调用此方法时,都会创建一个新的子视图控制器,可以吗?或者如何改进它?

    我的方法是将
    UICollectionViewCell
    子类化,并将所需的UI组件作为IVAR添加到其中。当您需要用新数据更新UI时,您可以像现在在伪代码中一样获取单元格对象,然后调用您声明的方法,可以是
    updateCellWithModel:
    ,并将数据源中保存的模型传递给它。在这个方法中,您将对UI元素是否创建进行一些简单的检查,并在需要时创建它们。在这些类型的方法中进行健全性检查总是一个好主意,但是元素应该在init方法中创建,并且始终存在


    编辑:我相信我回答了你的问题,但这仍然让我感到困惑,请添加更多信息,以便我可以在需要时编辑我的答案。

    我最近遇到过类似情况,并在各种解决方案之间进行了艰难选择,如中所述。现在似乎有一个开源项目封装了此模式:。如果您的问题可以通过UIViewController的UICollectionView得到最好的解决,那么它比您自己的更容易实现。

    我不认为他们在这里使用带有嵌入式表视图的集合视图单元格。它看起来像一个专用于管脚的普通视图控制器。从服务器为所述pin提取的数据可能包含关于pin的名称、pin的链接、可能的图像、添加pin的所有人、原始pin人是谁、喜好、收藏夹等的信息,并且视图控制器相应地解析该数据。控制器使用该数据更新UI,仅此而已


    我确实看到Pinterest可以用于实现此视图控制器的表视图和集合视图,但看起来它们并没有将所有这些数据嵌入到集合视图单元格中。

    gotcha,但如何处理按钮(如“Added by XXX”)事件?如果使用子vc,则可以在那里处理按钮事件。但是如果作为单元格的子视图属性添加,则应该在
    UICollectionViewController
    中处理它,对吗?所以一个fat控制器诞生了。handle视图的触摸事件是控制器职责的一部分,所以没关系。