在没有Storyboard的情况下在UIView中添加UICollectionView

时间:2017-06-04 15:01:02

标签: ios swift uiview uicollectionview uicollectionviewdelegate

我的ViewController名为 myVC ,带有UITablewView - myTable

我想要的是从代码 添加一些UIView作为 myTable的 headerView 。所以在 myVC 的viewDidLoad()方法中我添加了这段代码

    let topView = TopView()
    topView.frame.size.height = 100
    topView.frame.size.width = myTable.frame.width
    myTable.tableHeaderView = featuredEventsView

我还创建了名为TopView.swift的文件,看起来像

class TopView : UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)            
        self.backgroundColor = .red
    }

    required init?(coder aDecoder: NSCoder) {.....}
}

它正在按预期工作。我在 myTable 的headerView中看到了红色UIView。

现在我想在 topView 中添加UICollectionView,我在这里遇到了问题。我正在尝试做类似

的事情
class TopView : UIView, UICollectionViewDataSource, UICollectionViewDelegate {
    override init(frame: CGRect) {
        super.init(frame: frame)            
        self.backgroundColor = .red

        addSubview(myCollectionView)
    }

    required init?(coder aDecoder: NSCoder) {.....}

let myCollectionView : UICollectionView = {
        let cv = UICollectionView()
        cv.translatesAutoresizingMaskIntoConstraints = false
        cv.delegate = self as! UICollectionViewDelegate
        cv.dataSource = self as! UICollectionViewDataSource
        cv.backgroundColor = .yellow
        return cv
    }()
}

我还创建了UICollectionViewDataSource所需的函数,但构建后应用程序崩溃了。我做错了什么?

1 个答案:

答案 0 :(得分:1)

你有两个问题:

1)您必须错误地初始化UICollectionView,因为您必须为其布局。你需要这样的东西(使用你想要的任何框架,但如果你继续使用自动布局,它并不重要):

let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)

2)初始化属性时,不能在闭包内引用“self”。这是因为如果可能尚未初始化(如本例所示),那么您无法保证使用它是安全的。

我认为如果你像这样使用延迟初始化你应该没问题(加上你甚至不需要施放'self'):

lazy var myCollectionView : UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    let cv = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
    cv.translatesAutoresizingMaskIntoConstraints = false
    cv.delegate = self
    cv.dataSource = self
    cv.backgroundColor = .yellow
    return cv
}()

使用延迟方法应该延迟到self初始化,因此可以安全使用。