2017-03-29 25 views
0

我创建的故事板我自己的窗口,如下图所示:UICollectionView编程

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     // Override point for customization after application launch. 
     window = UIWindow(frame: UIScreen.main.bounds) 
     window?.makeKeyAndVisible() 
     window?.rootViewController = UINavigationController(rootViewController: HomeController()) 
     return true 
    } 

其实,这工作得很好,当我的类继承的UIViewController。

class HomeController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     view.backgroundColor = UIColor.red 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


} 

现在,当我继承我的类UICollectionViewController

class HomeController: UICollectionViewController { 

     override func viewDidLoad() { 
      super.viewDidLoad() 
      // Do any additional setup after loading the view, typically from a nib. 
      Collectionview.backgroundColor = UIColor.red 
     } 

     override func didReceiveMemoryWarning() { 
      super.didReceiveMemoryWarning() 
      // Dispose of any resources that can be recreated. 
     } 


    } 

在运行时,它给了我一个错误说:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter' *** First throw call stack:

在我的理解它要求我给布局UICollectionView。如果我错了,请在这里更正我。所以,我想这 - :

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     // Override point for customization after application launch. 
     window = UIWindow(frame: UIScreen.main.bounds) 
     window?.makeKeyAndVisible() 
     let layout = UICollectionViewFlowLayout() 
     window?.rootViewController = UINavigationController(rootViewController: HomeController(UICollectionViewLayout: layout)) 
     return true 
    } 

但仍结束了一个错误:

Argument labels 'UICollectionViewLayout' do not match any available overloads.

我使用了错误的swift3语法?如果是的话解决这个问题是正确的。

回答

1

您需要使用UICollectionViewController的指定启发器,如下所示。

let layout = UICollectionViewLayout() 
let homeViewController = HomeController(collectionViewLayout: layout) 
window?.rootViewController = UINavigationController(rootViewController: homeViewController) 

伴随着这一点,你需要修改你HomeViewController实现UICollectionViewDataSource所需的方法。

override func viewDidLoad() { 
     super.viewDidLoad() 

     // Uncomment the following line to preserve selection between presentations 
     // self.clearsSelectionOnViewWillAppear = false 

     // Register cell classes 
     self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell") 

     // Do any additional setup after loading the view. 
    } 


// MARK: UICollectionViewDataSource 

    override func numberOfSections(in collectionView: UICollectionView) -> Int { 
     // #warning Incomplete implementation, return the number of sections 
     return 0 
    } 


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     // #warning Incomplete implementation, return the number of items 
     return 0 
    } 

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) 

     // Configure the cell 

     return cell 
    } 
+0

为什么我不能使用let layout = UICollectionViewFlowLayout()。 UICollectionViewFlowLayout是UICollectionViewLayout的子类,用于实现简单网格的内置布局。布局高度可定制。 –

+0

我只在答案中使用UICollectionViewFlowLayout。您使用的参数的名称是错误的。它应该是HomeController(collectionViewLayout:layout)而不是HomeController(UICollectionViewLayout:layout)。 –

+0

对不起,我检查它,谢谢它的工作。 –

1

您需要在UICollectionViewController中实施UICollectionViewFlowLayout

入住这Link

必须使用initWithFrame:collectionViewLayout:初始化你UICollectionView,你必须通过一个非空UICollectionViewLayout object

相关问题