OGeek|极客世界-中国程序员成长平台

标题: ios - 在另一个 Collection View 的单元格内插入 Collection View [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:27
标题: ios - 在另一个 Collection View 的单元格内插入 Collection View

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

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

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

代码:

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


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

@end
@implementation RecipesViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpInitialView];
}

-(void)setUpInitialView{
    recipeTypeArray = [[NSMutableArray alloc]initWithObjects"ALL",@"ROTEIN VEGGIE",@"FRUIT",@"FAST FUEL",@"CARB",@"VEGETERIAN",@"SEA FOOD",@"CHICKEN", nil];
    _images = [@[@"bg-sidebarProfile.jpg"]mutableCopy];
   [self.mainCollectionView registerNib:[UINib nibWithNibName"MainCollectionViewCell" bundle:nil] forCellWithReuseIdentifier"MainCollectionViewCell"];

}

-(NSInteger)collectionViewUICollectionView *)collectionView numberOfItemsInSectionNSInteger)section{
    return recipeTypeArray.count;
}

-(UICollectionViewCell *)collectionViewUICollectionView *)collectionView cellForItemAtIndexPathNSIndexPath *)indexPath{
    if(collectionView == _horizontalCollectionView){
    cell = [collectionView dequeueReusableCellWithReuseIdentifier"cell" forIndexPath:indexPath];
        [cell.recipeTypeBtn setTitle:[recipeTypeArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];
        return cell;

    }
    else {
       verticalCell= [collectionView dequeueReusableCellWithReuseIdentifier"cvCell" forIndexPath:indexPath];
        verticalCell.commentTextView.layer.borderColor = [UIColor blackColor].CGColor;
        verticalCell.commentTextView.layer.borderWidth = 0.5;
        _verticalCollectionView.pagingEnabled = true;
        return verticalCell;

   }
//    else{
//        if(mainCell==nil){
//       mainCell= [_mainCollectionView dequeueReusableCellWithReuseIdentifier"MainCollectionViewCell" forIndexPath:indexPath];
//        }
//        return mainCell;
//    }

}


- (CGSize)collectionViewUICollectionView *)collectionView
                  layoutUICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPathNSIndexPath *)indexPath
{
    if(collectionView ==_verticalCollectionView){
    return collectionView.bounds.size;
    }
    else{
        return CGSizeMake(150, 120);
    }
}




@end



Best Answer-推荐答案


看到Collectionview变成一个CollectionView单元格会是这样的

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: 
     "FlashDealItemView", bundle: nil), forCellWithReuseIdentifier: 
     "flashdealitem")

}



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

func collectionView(_ collectionView: UICollectionView, cellForItemAt 
indexPath: IndexPath) -> UICollectionViewCell {

    let m:FlashDealItemCollectionViewCell = 
   collectionView.dequeueReusableCell(withReuseIdentifier: 
 "flashdealitem", for: indexPath) as! FlashDealItemCollectionViewCell

    return m;
}

func collectionView(_ collectionView: UICollectionView, layout 
 collectionViewLayout: UICollectionViewLayout, sizeForItemAt 
 indexPath: IndexPath) -> 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()


}

关于ios - 在另一个 Collection View 的单元格内插入 Collection View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47813650/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4