菜鸟教程小白 发表于 2022-12-11 19:27:02

ios - 在另一个 Collection View 的单元格内插入 Collection View


                                            <p><p>我正在尝试实现一个水平和垂直滚动都可以工作的 View 。为此,我采用了 Main CollectionView 。我把它的布局设置为水平的。在该 Collection View 的单元格中,我采用了另一个 Collection View ,我将其布局视为垂直。当我尝试运行它时,它会给出错误</p>

<pre><code>2017-12-14 19:02:09.518532+0530 FoodFuels *** Terminating app due to uncaught exception &#39;NSInternalInconsistencyException&#39;, reason: &#39;Could not load NIB in bundle: &#39;NSBundle &lt;/Users/abc/Library/Developer/CoreSimulator/Devices/EA4BBA47-DB91-4C91-B332-D3D6BD5C8698/data/Containers/Bundle/Application/1E7716C3-08F8-4AC6-8106-B961EB8E1076/FoodFuels.app&gt; (loaded)&#39; with name &#39;MainCollectionViewCell&#39;&#39;
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010bd241ab __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010b3c1f41 objc_exception_throw + 48
    2   CoreFoundation                      0x000000010bd98cb5 + + 197
    3   UIKit                               0x000000010cd3b11c - + 501
    4   UIKit                               0x000000010d38eb2b - + 998
    5   UIKit                               0x000000010d38f39b - + 169
    6   FoodFuels                           0x0000000109f3c5c8 - + 1096
    7   UIKit                               0x000000010d3785ea - + 290
    8   UIKit                               0x000000010d3784c2 - + 35
    9   UIKit                               0x000000010d37d9df - + 4775
    10UIKit                               0x000000010d3838d4 - + 364
    11UIKit                               0x000000010c9816f5 - + 1439
    12QuartzCore                        0x000000010a9e03ee - + 153
    13QuartzCore                        0x000000010a9e44dd _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 401
    14QuartzCore                        0x000000010a96cded _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 365
    15QuartzCore                        0x000000010a998704 _ZN2CA11Transaction6commitEv + 500
    16UIKit                               0x000000010c8daca3 _afterCACommitHandler + 272
    17CoreFoundation                      0x000000010bcc6d37 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    18CoreFoundation                      0x000000010bcc6c8e __CFRunLoopDoObservers + 430
    19CoreFoundation                      0x000000010bcab254 __CFRunLoopRun + 1572
    20CoreFoundation                      0x000000010bcaa9b9 CFRunLoopRunSpecific + 409
    21GraphicsServices                  0x000000011238c9c6 GSEventRunModal + 62
    22UIKit                               0x000000010c8b05e8 UIApplicationMain + 159
    23FoodFuels                           0x0000000109f3e4ff main + 111
    24libdyld.dylib                     0x000000010fa0fd81 start + 1
    25???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) .
</code></pre>

<p>我已经尝试了网上所有可能的解决方案,但我的情况还没有任何解决方案。</p>

<p>代码:</p>

<pre><code>#import &#34;RecipesViewController.h&#34;
#import &#34;DemoNavigationController.h&#34;
#import &#34;NewSideViewController.h&#34;
#import &#34;RecipesCategoriesCollectionViewCell.h&#34;
#import &#34;RecipesTableViewCell.h&#34;
#import &#34;RecipesHorizontalCell.h&#34;
#import &#34;VerticalCollectionViewCell.h&#34;
#import &#34;MainCollectionViewCell.h&#34;
@interface RecipesViewController () &lt;UICollectionViewDataSource,UICollectionViewDelegate&gt; {
    NSMutableArray *recipeTypeArray;
    RecipesHorizontalCell *cell;
    VerticalCollectionViewCell *verticalCell;
    MainCollectionViewCell *mainCell;


}
@property (strong, nonatomic) NSMutableArray *images;

@end
@implementation RecipesViewController
- (void)viewDidLoad {
    ;
    ;
}

-(void)setUpInitialView{
    recipeTypeArray = [initWithObjects:@&#34;ALL&#34;,@&#34;PROTEIN VEGGIE&#34;,@&#34;FRUIT&#34;,@&#34;FAST FUEL&#34;,@&#34;CARB&#34;,@&#34;VEGETERIAN&#34;,@&#34;SEA FOOD&#34;,@&#34;CHICKEN&#34;, nil];
    _images = [@[@&#34;bg-sidebarProfile.jpg&#34;]mutableCopy];
    forCellWithReuseIdentifier:@&#34;MainCollectionViewCell&#34;];

}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return recipeTypeArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if(collectionView == _horizontalCollectionView){
    cell = ;
       forState:UIControlStateNormal];
      return cell;

    }
    else {
       verticalCell= ;
      verticalCell.commentTextView.layer.borderColor = .CGColor;
      verticalCell.commentTextView.layer.borderWidth = 0.5;
      _verticalCollectionView.pagingEnabled = true;
      return verticalCell;

   }
//    else{
//      if(mainCell==nil){
//       mainCell= ;
//      }
//      return mainCell;
//    }

}


- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(collectionView ==_verticalCollectionView){
    return collectionView.bounds.size;
    }
    else{
      return CGSizeMake(150, 120);
    }
}




@end
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>看到Collectionview变成一个CollectionView单元格会是这样的</p>

<pre><code>protocol FlashDealCollectionViewCellDelegate:NSObjectProtocol {
func onFlashDealItemClicked();
}
class FlashDealCollectionViewCell: UICollectionViewCell ,
UICollectionViewDelegate, UICollectionViewDelegateFlowLayout,
UICollectionViewDataSource{


//for handle click event using delegate
weak var delegate: FlashDealCollectionViewCellDelegate?

override func awakeFromNib() {
    super.awakeFromNib()
    serInnerCollectionView()
}

func serInnerCollectionView() {


    flashDealCollectionView.delegate = self
    flashDealCollectionView.dataSource = self
    flashDealCollectionView.register(UINib(nibName:
   &#34;FlashDealItemView&#34;, bundle: nil), forCellWithReuseIdentifier:
   &#34;flashdealitem&#34;)

}



func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -&gt; Int {
   return 25
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt
indexPath: IndexPath) -&gt; UICollectionViewCell {

    let m:FlashDealItemCollectionViewCell =
   collectionView.dequeueReusableCell(withReuseIdentifier:
&#34;flashdealitem&#34;, for: indexPath) as! FlashDealItemCollectionViewCell

    return m;
}

func collectionView(_ collectionView: UICollectionView, layout
collectionViewLayout: UICollectionViewLayout, sizeForItemAt
indexPath: IndexPath) -&gt; CGSize {
    let width = 80;

    return CGSize(width: width+10, height: 100);
}


func collectionView(_ collectionView: UICollectionView,
didSelectItemAt indexPath: IndexPath) {



    guard let delegate = delegate else {
      return
    }

    //Handle click event
    delegate.onFlashDealItemClicked()


}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在另一个 Collection View 的单元格内插入 Collection View ,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/47813650/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/47813650/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在另一个 Collection View 的单元格内插入 Collection View