UICollectionViewCell 无法注册笔尖?

UICollectionViewCell unable to register nib?

viewcontroller 的结构是:

-ViewController

-- TableView(在同一个故事板中 - viewcontroller)

--- TableViewCell(在不同的 xib 文件中)

---- CollectionView(在 TableViewCell xib 内)

----- CollectionViewCell(在不同的 Xib 文件中)

我的问题是我总是收到这个错误:

2017-07-14 13:49:36.752763+0300 muzeit[10370:3481928] *** Assertion failure in -[UITableView _dequeueReusableViewOfType:withIdentifier:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3600.8.1/UITableView.m:6696
2017-07-14 13:49:36.755821+0300 muzeit[10370:3481928] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'invalid nib registered for identifier (tbc_horizontal_songs) - nib must contain exactly one top level object which must be a UITableViewCell instance'
*** First throw call stack:
(0x188f0afe0 0x18796c538 0x188f0aeb4 0x1899a2720 0x18f179768 0x18f1b3cc8 0x100236478 0x100236630 0x18f3811f8 0x18f381410 0x18f36eb14 0x18f386400 0x18f11e858 0x18f03907c 0x18c229274 0x18c21dde8 0x18f04d814 0x18f173a50 0x18f1737f0 0x18f172a9c 0x18f172820 0x18f17267c 0x18f172350 0x10025c9c0 0x10025cb94 0x18f053bf4 0x18f053964 0x18f0f3458 0x18f0f2c2c 0x18f0f27e0 0x18f0f2744 0x18f03907c 0x18c229274 0x18c21dde8 0x18c21dca8 0x18c19934c 0x18c1c03ac 0x18c1c0e78 0x188eb89a8 0x188eb6630 0x188de6dc4 0x18f0a6384 0x18f0a1058 0x1001e97bc 0x187df559c)
libc++abi.dylib: terminating with uncaught exception of type NSException

TableViewCell swift 文件内容:

class tbc_horizontal_songs: UITableViewCell {


    @IBOutlet weak var collectionView : UICollectionView!

    override func awakeFromNib() {
        super.awakeFromNib()

    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
}


extension tbc_horizontal_songs : UICollectionViewDataSource {

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 12
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cvc_song", for: indexPath as IndexPath) as! cvc_song
        return cell
    }
}

extension tbc_horizontal_songs : UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        let itemsPerRow:CGFloat = 4
        let hardCodedPadding:CGFloat = 5
        let itemWidth = (collectionView.bounds.width / itemsPerRow) - hardCodedPadding
        let itemHeight = collectionView.bounds.height - (2 * hardCodedPadding)
        return CGSize(width: itemWidth, height: itemHeight)

    }

}

因为当我在 tableviewcell 的分离文件中添加 collectionview 时,collectionViewCell 不会出现,我必须添加自定义的,我无法注册 collectionViewCell 的笔尖,知道吗?

你需要在 viewcontroller 中注册 tableview cell 并在 table cell 的 awakefrom nib 中注册 collectionview cell as follws

tableview.register(UINib(nibName: "nibname", bundle: nil), forCellWithReuseIdentifier: "identifier")

 override func awakeFromNib() {
    super.awakeFromNib()
    collectionView.register(UINib(nibName: "collectioncellnibname", bundle: nil), forCellWithReuseIdentifier: "cvc_song")

 }