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