在另一个集合视图的单元格内插入集合视图
Posted
技术标签:
【中文标题】在另一个集合视图的单元格内插入集合视图【英文标题】:Inserting collectionview inside cell of another collection view 【发布时间】:2017-12-14 12:41:29 【问题描述】:我正在尝试实现水平和垂直滚动都可以工作的视图。为此,我采用了 Main Collection 视图。我把它的布局设置为水平的。在该集合视图的单元格中,我采用了另一个集合视图,我将其布局视为垂直。当我尝试运行它时,它会给出错误
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 instantiateWithOwner:options:] + 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",@"PROTEIN 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)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
return recipeTypeArray.count;
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)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)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
if(collectionView ==_verticalCollectionView)
return collectionView.bounds.size;
else
return CGSizeMake(150, 120);
@end
【问题讨论】:
你能发布你的主要 CollectionView 初始化和它的单元格的 awakeFromNIb 我什么都没做,只是注册了笔尖并尝试在 cellforrowatindexpath 数据源方法中返回单元格。 为简单起见,尝试将单元格中的collectionView替换为tableView,因为它是垂直的 我已经做了横向的事情,只是停留在一个问题上……为此我必须从头开始。 问题可能在于加载内部单元格,因此无法加载主单元格 【参考方案1】:看到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()
【讨论】:
已经这样做了,但没有结果。 [self.mainCollectionView registerNib:[UINib nibWithNibName:@"MainCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"MainCollectionViewCell"]; 你的ReuseIdentifier设置完美了吗?请检查 是的,都具有相同的名称、单元格和可重复使用的标识符。 正如我所注意到的,你添加的一个新东西是代理......这也是在 didselect..但我的屏幕在它出现之前就崩溃了。 请粘贴崩溃报告【参考方案2】:取消这样的评论
else
MainCollectionViewCell*mainCell= [_mainCollectionView dequeueReusableCellWithReuseIdentifier:@"MainCollectionViewCell" forIndexPath:indexPath];
return mainCell;
【讨论】:
以上是关于在另一个集合视图的单元格内插入集合视图的主要内容,如果未能解决你的问题,请参考以下文章