Исключение: «NSInternalInconsistencyException», причина: «Не удалось загрузить NIB в комплекте:« NSBundle ..... в Swift 4 » - PullRequest
0 голосов
/ 24 мая 2018

Это мой View Controller, в котором я установил свой collectionView и его методы.

class HomeScreenViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {


@IBOutlet weak var collectionViewOne: UICollectionView!

override func viewDidLoad() {
    super.viewDidLoad()
     collectionViewOne.register(UINib(nibName: "recomendedcell1", bundle: nil), forCellWithReuseIdentifier: "cell1")
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 10
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = Bundle.main.loadNibNamed("recommendedcell1", owner: self, options: nil)?.first as! CustomCellOneCollectionViewCell
    return cell
}

Это мой пользовательский файл ячейки

class CustomCellOneCollectionViewCell: UICollectionViewCell {

@IBOutlet weak var imageView: UIImageView!
override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

}

Я также использовал приведенный ниже код для объявления ячейки.

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! CustomCellOneCollectionViewCell

Я проверил все соединения, дважды проверил имена идентификаторов.Контроллер моего представления является частью контроллера панели вкладок.

Ответы [ 4 ]

0 голосов
/ 24 мая 2018

Во время регистрации пера nibName shoudbe имя ячейки означает CustomCellOneCollectionViewCell

collectionViewOne.register(UINib(nibName: "CustomCellOneCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "cell1")
collectionViewOne.delegate = self
collectionViewOne.datasource = self

, а в cellForItemAt

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! CustomCellOneCollectionViewCell
    return cell
}
0 голосов
/ 24 мая 2018
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell : MyCustomView = collectionView.dequeueReusableCellWithReuseIdentifier("your_reusable_identifier", forIndexPath: indexPath) as! CustomCellOneCollectionViewCell

    return cell
}
0 голосов
/ 24 мая 2018

удалить эту строку

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = Bundle.main.loadNibNamed("recommendedcell1", owner: self, options: nil)?.first as! CustomCellOneCollectionViewCell
        return cell
}

попробуйте эту

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell  = collectionView.dequeueReusableCell(withReuseIdentifier: "recommendedcell1", for: indexPath) as!  CustomCellOneCollectionViewCell
        return cell

}
0 голосов
/ 24 мая 2018

Убедитесь, что ваша ячейка recommendedcell1 Убедитесь, что ее суперкласс имеет тип CustomCellOneCollectionViewCell

enter image description here

Почему эта строка

let cell = Bundle.main.loadNibNamed("recommendedcell1", owner: self, options: nil)?.first as! CustomCellOneCollectionViewCell

просто dequeueReusableCell

          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! CustomCellOneCollectionViewCell

Так что ваша клетка будет

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
   let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! CustomCellOneCollectionViewCell
    return cell
}
...