2016-06-21 118 views
0

我想在Swift中仅使用代码制作UICollectionView。我删除了Main.story并完成了所有设置。它运行良好,但单元格从不显示。我在YouTube上跟着一个教程,我发誓我的代码是完全一样的(除了变量名或其他),但它永远不会显示单元格。如果任何人都能弄清楚为什么,我将不胜感激。这是ViewController文件,在底部添加了AppDelegate文件。我可以更改整个控制器的背景,但更改单元格背景颜色不会执行任何操作。在Swift代码中的UICollectionView?为什么我的代码不能工作?

import UIKit 

class CustomCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 

    let customCellIdentifier = "customCellIdentifier" 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     collectionView?.backgroundColor = .greenColor() 

     collectionView?.registerClass(CustomCell.self, forCellWithReuseIdentifier: customCellIdentifier) 
    } 

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 3 
    } 

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let customCell = collectionView.dequeueReusableCellWithReuseIdentifier(customCellIdentifier, forIndexPath: indexPath) 
     return customCell 
    } 

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
     return CGSizeMake(view.frame.width, 100) 
    } 


} 

class CustomCell: UICollectionViewCell { 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
     setupViews() 
     } 

    let nameLabel: UILabel = { 
     let label = UILabel() 
     label.text = "Custom Text" 
     label.translatesAutoresizingMaskIntoConstraints = false 
     return label 
    }() 

    func setupViews() { 
     backgroundColor = UIColor.redColor() 
     addSubview(nameLabel) 
     addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel])) 
     addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel])) 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

} 


import UIKit 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

     window = UIWindow(frame: UIScreen.mainScreen().bounds) 
     window?.makeKeyAndVisible() 

     let flowLayout = UICollectionViewLayout() 
     let customCollectionViewController = CustomCollectionViewController(collectionViewLayout: flowLayout) 

     window?.rootViewController = UINavigationController(rootViewController: customCollectionViewController) 

     // Override point for customization after application launch. 
     return true 
    } 

回答

2

iOS documentation here摘自:

的UICollectionViewLayout类是继承和使用,产生的集合布局信息的抽象基类。布局对象的工作是确定集合视图边界内的单元格,补充视图和装饰视图的位置,并在被询问时将该信息报告给集合视图。集合视图然后将提供的布局信息应用于相应的视图,以便它们可以在屏幕上呈现。

您必须继承UICollectionViewLayout才能使用它。然而,在考虑子类化之前,您应该查看 UICollectionViewFlowLayout类,以了解它是否可以适应您的布局需求。

那么你或许应该改变

let flowLayout = UICollectionViewLayout() 

let flowLayout = UICollectionViewFlowLayout() 
相关问题