2017-02-12 31 views
0

我真的很困惑尝试init()我的自定义单元类中的一些东西,我想成为一个collectionview的数据源和委托。init()是自定义单元类中的委托和数据源的collectionView

我如何初始化(),以便我有数据中的数据准备被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") 
} 
+0

您无法创建自定义初始程序。您需要实施所需的初始程序并提供一些其他功能来加载数据。 – Paulw11

+0

只是在重新加载您的单元格对象collectionview之前分配数据。 – iAviator

+0

我不明白这是什么意思..在这种情况下从哪里重新加载数据?如果单元格是嵌套collectionView的数据源,那么需要在这里分配数据? – Pippo

回答

0

你不能创建自己的初始化了的UITableViewCell。唯一可以覆盖的两个初始值设定项是一个接收帧(init(frame))和一个接收代码(init(coder))的初始值设定项。

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

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

相关问题