将UICollectionViewController添加到UITabbarController时出现异常

时间:2015-06-12 16:43:16

标签: ios uicollectionview uitabbarcontroller collectionview

我是iOS编程的新手,希望你能帮助我。

我得到了

  

' NSInvalidArgumentException'

尝试将UICollectionViewController添加到我的UITabbarController中时..

  必须使用非零布局参数初始化

' UICollectionView'

问题是我用FlowLayout初始化了我的CollectionView ......这里的代码是MatchesCell是我的UICollectionViewCell实现

[self.collectionView registerClass:[MatchesCell class] forCellWithReuseIdentifier:@"MatchesCell"];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(200, 140)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
[self.collectionView initWithFrame:[self.tabBarController.view frame]  collectionViewLayout:flowLayout];`

我也尝试用[[UIScreen mainScreen] bounds]或我手动创建的CGRect调用initWithFrame,也没有工作......请帮忙!

提前致谢!

2 个答案:

答案 0 :(得分:1)

看起来您可能正在初始化两个UICollectionViewControllers。你的代码中的第一行是[self.collectionView registerClass:...],这意味着在你到达代码的最后一行之前已经存在一行[self.collectionView initWithFrame:.. collectionViewLayout:..]。

如果您要在故事板中进行集合视图,请仔细检查它在其属性中是否设置了有效的布局,您可以在界面构建器的右侧面板中找到该属性。否则,在获得此代码之前,请仔细检查代码的其余部分,以便创建UICollectionView。任何init方法,viewDidLoad,viewWillAppear或viewDidAppear都可能是一个很好的起点。

最后,尝试添加一个异常断点,希望能够显示应用崩溃的确切行。 https://developer.apple.com/library/ios/recipes/xcode_help-breakpoint_navigator/articles/adding_an_exception_breakpoint.html

答案 1 :(得分:-1)

CollectionViewController初始化应如下所示,

 UICollectionViewLayout *myLayout = [[UICollectionViewLayout alloc] init];        
CollectionViewController *collectionViewController = [[CollectionViewController alloc] initWithCollectionViewLayout:myLayout];

在&{34; viewDidLoad"等CollectionViewController生命周期方法中,添加以下内容,

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];        
self.view.backgroundColor = [UIColor whiteColor];    
self.collectionView.backgroundColor = [UIColor clearColor];        
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];        
flowLayout.itemSize = CGSizeMake(CGRectGetWidth(self.collectionView.frame)/2 - 20,     
CGRectGetWidth(self.collectionView.frame)/2 - 20);
self.collectionView.collectionViewLayout = flowLayout;

这应该可以解决您的问题。 感谢