如何在没有ViewController作为所有者的情况下实例化笔尖?

时间:2018-12-10 16:10:52

标签: swift uicollectionviewcell xctest

我有一个UICollectionViewCell的子类,我想自己对其进行单元测试。在没有所有者的情况下,如何单独实例化它(有可能?)?

1 个答案:

答案 0 :(得分:1)

有可能。在此代码中,我将假设您的单元格类名称为:CustomCollectionViewCell,并且位于CustomCollectionViewCell.xib文件中:

let bundle = Bundle(for:CustomCollectionViewCell.self)
let nib = UINib(nibName: "CustomCollectionViewCell", bundle: bundle)
//nibName should be the same as your file name
let cell = nib.instantiate(withOwner: nil, options: nil).first as? CustomCollectionViewCell
相关问题