【问题标题】:NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'NSInvalidArgumentException',原因:'UICollectionView 必须使用非零布局参数初始化'
【发布时间】:2013-06-20 13:44:26
【问题描述】:

错误信号 SIGABRT NSInvalidArgumentException',原因:'UICollectionView 必须使用非零布局参数初始化'。

当 UITableViewCell 被按下时,它应该显示 UICollectionView

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIViewController *viewController;
switch (indexPath.row) {

    case IMAGE: 
        viewController = [[[CollectionViewController alloc] init] autorelease];
 [self.CollectionViewController registerClass:[UICollectionViewCell class]forCellWithReuseIdentifier:@"Cell"];

        case PROCEDURAL:
        viewController = [[[ProceduralExampleViewController alloc] init] autorelease]; 
        break;
    default: 
        viewController = [[[UIViewController alloc] init] autorelease];
} 
[self.navigationController pushViewController:viewController animated:YES];
}

使用故事板文件进行集合视图segue。

如何解决这个错误UICollectionView 必须用非零布局参数初始化

感谢您的帮助。

【问题讨论】:

  • 也许你应该看看这个类似的问题:stackoverflow.com/questions/15270058/…
  • 你是如何合成你的 UICollectionView 的?
  • 在tableviewcontroller中声明uicollectionview的属性不是IBOutlet它
  • 昨天试过这样的代码 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UIViewController *viewController;switch (indexPath.row) {case IMAGE: viewController = [ [[CollectionViewController alloc] init] autorelease];[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"myCell"];但没有成功

标签: iphone objective-c uitableview ios6 uicollectionview


【解决方案1】:

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

希望是不言自明的。 UICollectionView 的指定初始化器是

- (id)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout

所以你需要给它传递一个布局对象。您没有在创建集合视图的位置显示代码,但我假设 CollectionViewControllerUICollectionViewController 子类,在这种情况下,您需要使用这个指定的初始化程序:

- (id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout

目前,您使用的是init

在这两种情况下,您都需要在初始化时创建并传入布局对象。

如果您的集合视图保存在情节提要中,那么您需要使用 instantiateViewControllerWithIdentifier: 从情节提要中获取它,因此您上面的代码将如下所示:

viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"CollectionViewController"];

【讨论】:

  • 我试图在按下 tableview 单元格时显示 uicollection 视图。我有用于 uicollectionviewcontroller segue 的集合视图控制器类和情节提要。我如何在 tableview didSelectRowAtIndexPath 中初始化 collectionview。如果你能告诉我。
  • 我确实添加了那一行,现在它说应用程序试图在目标 上推送一个 nil 视图控制器。我应该将 mainstoryboard 导入到 tableviewcontroller
  • 好吧,我对你的故事板或上面的标识符一无所知。您需要在情节提要上为集合视图控制器设置一个标识符,否则它不会返回任何内容。
猜你喜欢
  • 2014-08-08
  • 2019-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-09
  • 2019-01-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多