init()在自定义单元类中,它是delegView的委托和数据源

时间:2017-02-12 10:07:48

标签: ios swift uicollectionview init

我真的很困惑尝试init()我的自定义单元格类中的一些东西,我想成为集合视图的数据源和委托。

我如何init()以便我有一个数组准备好被cellForItemAt使用?

var partArray : [CollectionStruct] = []

init(partArray: [CollectionStruct]) {
super.init(partArray: [CollectionStruct])


    innerCollectionView.delegate = self
    innerCollectionView.dataSource = self
    //innerCollectionView.tag = item

    // add some stuff from local sql lite to array
    // this is how i normally do this in viewDidLoad
    // but cant use that in cell subclass
    BuildArray.buildArrayFromQuery(queryForCollection: "Part", delegateSender: "DownloadPack", completeBlock: { (result) in
        if result.isEmpty == false {
            self.partArray = result
        }
    })
}

如果我这样做:

init() {
// stuff for the array
}

我迅速建议我加入这个区块:

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

1 个答案:

答案 0 :(得分:0)

您无法为UITableViewCell创建自己的初始值设定项。您可以覆盖的唯一两个初始化程序是一个接收框架(init(frame))和一个接收代码(init(coder))的初始化程序。

为了找到问题的解决方案,您可以使用属性或接收数据数组的方法。在创建单元格之后,将调用或设置任一个。您进行设置,然后调用collectionView的reloadData()

如果您决定使用某个属性,则可以在该属性的didSet中执行此提及的逻辑。

相关问题