gpt4 book ai didi

ios - UICollectionViewCell 无法注册 Nib ?

转载 作者:行者123 更新时间:2023-12-01 19:52:12 25 4
gpt4 key购买 nike

View Controller 的结构是:

- View Controller

- 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,知道吗?

最佳答案

您需要在 viewcontroller 中注册 tableview 单元格,并在 awakefrom 表格单元格的 nib 中注册 collectionview 单元格,如下所示

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

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

}

关于ios - UICollectionViewCell 无法注册 Nib ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45101188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com