UICollectionViewCell无法注册nib?

时间:2017-07-14 10:52:44

标签: ios xcode uitableview collectionview

viewcontroller的结构是:

-ViewController

- TableView(在同一个storyboard中 - 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的nib,任何想法?

1 个答案:

答案 0 :(得分:1)

你需要在表格单元格的awakefrom nib中的viewcontroller和collectionview单元中注册tableview单元格

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

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

 }