Swift代码中的UICollectionView?为什么我的代码不起作用?

时间:2016-06-21 14:37:31

标签: ios swift

我正在尝试仅使用代码在Swift中创建UICollectionView。我删除了Main.story并完成了所有设置。它运行良好,但细胞永远不会显示。我在YouTube上关注了一个教程,我发誓我的代码完全相同(除了变量名或其他),但它不会显示单元格。如果有人能弄明白为什么,我会非常感激。这是ViewController文件,在底部我添加了AppDelegate文件。我可以更改整个控制器的背景,但更改单元格背景颜色不起作用。

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
    }

1 个答案:

答案 0 :(得分:2)

取自iOS documentation here

  

UICollectionViewLayout类是一个抽象基类,您可以将其子类化并用于生成集合视图的布局信息。布局对象的工作是确定集合视图边界内的单元格,补充视图和装饰视图的位置,并在询问时将该信息报告给集合视图。然后,集合视图将提供的布局信息应用于相应的视图,以便可以在屏幕上显示它们。

     

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

所以你可能应该改变

let flowLayout = UICollectionViewLayout()

let flowLayout = UICollectionViewFlowLayout()