在没有故事板的 UIView 中添加 UICollectionView [英] Adding UICollectionView inside UIView without Storyboards

查看:27
本文介绍了在没有故事板的 UIView 中添加 UICollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have ViewController called myVC with UITablewView - myTable.

我想要的是添加一些 UIView 作为 myTable's headerView from code.所以在 myVC 的 viewDidLoad() 方法中,我添加了这段代码

What I want is to add some UIView as myTable's headerView from code. So inside viewDidLoad() method of myVC I added this code

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

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

I also created file called TopView.swift that looks something like

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

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

它正在正常工作.我在 myTable 的 headerView 中看到红色的 UIView.

And it is working as it should. I see red UIView in headerView of myTable.

现在我想在 topView 中添加 UICollectionView 并且我在这里遇到了问题.我正在尝试做类似的事情

Now I want to add UICollectionView inside topView and I have problems here. I am trying to do something like

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 所需的函数,但应用程序在构建后崩溃.我做错了什么?

I also created functions needed to UICollectionViewDataSource but app crashes after building. What am I doing wrong?

推荐答案

你有两个问题:

1) 你不正确地初始化你的 UICollectionView,因为你必须给它一个布局.你需要这样的东西(使用任何你想要的框架,但如果你打算使用自动布局,那没关系):

1) You initialise your UICollectionView incorrectly as you must give it a layout. You need something like this (use whatever frame you want but if you are going on to use auto layout it doesn't matter):

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

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

2) You cannot reference 'self' inside the closure when initialising a property. This is because if may not have been initialised (as in this case) so you can't guarantee it's safe to use it.

我认为如果您使用这样的延迟初始化应该没问题(而且您甚至不需要强制转换self"):

I think you should be ok if you use lazy initialisation like this (plus you don't even need to cast '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 被初始化并因此可以安全使用.

Using the lazy method should delay until self is initialised and therefore safe to use.

这篇关于在没有故事板的 UIView 中添加 UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆