Почему мой вид коллекции дает неожиданно найденное нулевое значение? - PullRequest
0 голосов
/ 10 апреля 2020

Я попытался представить новый контроллер представления, и этот контроллер имеет представление коллекции. Когда я пытаюсь открыть этот контроллер представления, он дает потоку неожиданно найденный ноль в моем представлении коллекции, но все кажется хорошим, это должно работать, я не могу решить это. Почему это случилось? Вот мой код просмотра коллекции:

class catViewController: UIViewController {
    @IBOutlet weak var collectionViewCat: UICollectionView!



    override func viewDidLoad() {
        super.viewDidLoad()
        collectionViewCat.delegate = self
        collectionViewCat.dataSource = self
        fetchArticles()
    }


extension catViewController: UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.articles?.count ?? 0

    }

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

        cell.imgView.downloadImage(url: (self.articles?[indexPath.item].imageUrl)!)
        cell.title.text = self.articles?[indexPath.item].headline
        cell.url.text = self.articles?[indexPath.item].author

        return cell
    }


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let selectedNews = self.articles![indexPath.item].url
        print(selectedNews!)

        let webVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(identifier: "web") as! webviewViewController

        webVC.url = self.articles?[indexPath.item].url

        self.present(webVC, animated: true, completion: nil)

    }

}


// MARK: flow layout delegate

extension catViewController: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = collectionView.frame.size.width
        let height: CGFloat = 265
        return CGSize(width: width, height: height)
    }
}
...