使用故事板构建集合视图失败

时间:2014-12-18 15:40:40

标签: ios swift storyboard collectionview

我正在尝试通过故事板在标准视图控制器中构建集合视图。我没有使用集合视图控制器,因为我想在一个屏幕上看到几个集合视图。 视图控制器具有正确的类(statisticsViewController),并且Collection View连接到代码(作为outlet,dataSource和delegate)

我的课程代码如下:

import UIKit

class statisticsViewController :
UICollectionViewController,
UICollectionViewDelegateFlowLayout {

    @IBOutlet weak var collectionViewActive: UICollectionView!
    private let reuseIdentifier = "cellIdentifier"

    var items: [String] = ["Bli", "Bla", "Blubb", "Bla", "Blubb", "Bla", "Blubb", "Bla", "Blubb", "Bla", "Blubb", "Bla", "Blubb"]

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return items.count
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        println("ich war hier")
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.items.count;
    }


    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCell
        cell.backgroundColor = UIColor.blackColor()

        return cell
    }
}

当我尝试运行代码时,我收到以下错误消息:

2014-12-18 16:31:47.533 project[2008:92664] *** Assertion failure in -[project.statisticsViewController loadView], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UICollectionViewController.m:166
2014-12-18 16:31:47.536 project[2008:92664] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UICollectionViewController loadView] loaded the "UIr-oH-mfu-view-fG7-sl-ulu" nib but didn't get a UICollectionView.'
*** First throw call stack:

...

当我设置断点时,我发现viewDidLoad甚至没有被调用。

有人有想法吗? 谢谢你的帮助:))

1 个答案:

答案 0 :(得分:0)

只是为了回答这个问题:D

导入UIKit

类statisticsViewController: UIViewController中, UICollectionViewDelegateFlowLayout {

@IBOutlet weak var collectionViewActive: UICollectionView!
private let reuseIdentifier = "cellIdentifier"

var items: [String] = ["Bli", "Bla", "Blubb", "Bla", "Blubb", "Bla", "Blubb", "Bla", "Blubb", "Bla", "Blubb", "Bla", "Blubb"]

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return items.count
}

override func viewDidLoad() {
    super.viewDidLoad()
    println("ich war hier")
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.items.count;
}


func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCell
    cell.backgroundColor = UIColor.blackColor()

    return cell
}

}

现在有效!