【发布时间】:2020-12-06 18:05:19
【问题描述】:
我收到了这个错误,我检查了网上关于如何解决这个问题的每个答案,但它对我不起作用。我尝试清理构建文件夹,我没有 xib 文件,重新上传 collectionviewcell 文件,并且我拥有所有正确的代码。我有三个collectionviews,其中两个工作,而另一个不工作。这是我这个类的整个 collectionview 代码:
collectionViewThree 不工作,collectionView 和collectionViewTwo 完美工作! SleepThreeCollectionViewCell 确实存在!
@IBOutlet weak var collectionViewTwo: UICollectionView!
@IBOutlet weak var collectionViewThree: UICollectionView!
var arr = ["Quiet Time", "The Soft Lullaby", "", "", ""]
var imgArr = ["baby", "lulaby", "", "", ""]
var des = ["This is a soothing melody tone in for calming.", "Soft relaxing lullaby music featuring orchestra.", "", "", ""]
var arrTwo = ["Quiet Time", "The Soft Lullaby", "", "", ""]
var imgArrTwo = ["baby", "lulaby", "", "", ""]
var desTwo = ["This is a soothing melody tone in for calming.", "Soft relaxing lullaby music featuring orchestra.", "", "", ""]
var arrThree = ["Quiet Time", "The Soft Lullaby", "", "", ""]
var imgArrThree = ["baby", "lulaby", "", "", ""]
var desThree = ["This is a soothing melody tone in for calming.", "Soft relaxing lullaby music featuring orchestra.", "", "", ""]
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == self.collectionView {
return arr.count
}
else if collectionViewTwo == self.collectionView{
return arrTwo.count
}
return arrThree.count
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if self.collectionView == collectionView{
i = indexPath.row
let popupContentController = storyboard?.instantiateViewController(withIdentifier: "PlayerViewController") as! PlayerViewController
popupContentController.popupItem.title = arr[i]
popupContentController.popupItem.subtitle = des[i]
popupContentController.popupItem.image = UIImage(named: imgArr[i])
popupContentController.i = i
tabBarController?.presentPopupBar(withContentViewController: popupContentController, animated: false, completion: nil)
}else{
print("Still Working")
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if self.collectionView == collectionView{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "sleep", for: indexPath) as! SleepCollectionViewCell
cell.img.image = UIImage(named: imgArr[indexPath.row])
cell.label.text = arr[indexPath.row]
return cell
}else if self.collectionView == collectionViewTwo{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "sleepTwo", for: indexPath) as! SleepTwoCollectionViewCell
cell.imgTwo.image = UIImage(named: imgArrTwo[indexPath.row])
cell.labelTwo.text = arrTwo[indexPath.row]
return cell
}
else{
let cell: SleepThreeCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "third", for: indexPath) as! SleepThreeCollectionViewCell
cell.imgThree.image = UIImage(named: imgArrThree[indexPath.row])
cell.labelThree.text = arrThree[indexPath.row]
return cell
}
}
var i = 0
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
collectionViewTwo.delegate = self
collectionViewTwo.dataSource = self
collectionViewThree.register(UINib(nibName: "SleepThreeCollectionViewCell", bundle: Bundle(for: SleepThreeCollectionViewCell.self) ), forCellWithReuseIdentifier: "third")
collectionViewThree.delegate = self
collectionViewThree.dataSource = self
}
【问题讨论】:
-
为了什么目的你有这行 "collectionViewThree.register(UINib(nibName: "SleepThreeCollectionViewCell", bundle: Bundle(for: SleepThreeCollectionViewCell.self)), forCellWithReuseIdentifier: "third")" ?如果您使用情节提要中的此单元格,则应将其删除
-
我解决了这个问题,我没有创建多个集合视图,而是创建了一个表并将集合视图放在表中。
标签: swift xcode uicollectionview uicollectionviewcell nsbundle