Search code examples
iosswift4.2

Thread 1: EXC_BAD_ACCESS (code=2, address=0x16f913820)


I am trying to add a collectionView programatically to a subview.But when I return number of items as 1 ,collection is loading somany number of cells.Here I am using SharkORM in my project,Will it effects collectionView?.My actual requirement is I will be having files ,The collection of images need to store in relevant file.but this collection view is not loading required number of cells.Can anyone help to solve this.

func createCollection(){

        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.sectionInset = UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
        flowLayout.itemSize = CGSize(width: 82, height: 82)
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.scrollDirection = UICollectionView.ScrollDirection.vertical
        collectionView = UICollectionView(frame: CGRect(x: 10, y: 0, width: self.customView.frame.size.width-20, height: self.customView.frame.size.height), collectionViewLayout: flowLayout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(UINib(nibName: "ReviewCollectionCellPage", bundle: nil), forCellWithReuseIdentifier: "ReviewCollectionCellPage")
        collectionView.backgroundColor = UIColor.clear
        customView.addSubview(collectionView)

    }






extension ReviewViewController:UICollectionViewDelegate,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        print(finalImages.count)
        return finalImages.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
       // print(finalImages)
        let cell : ReviewCollectionCellPage = collectionView.dequeueReusableCell(withReuseIdentifier: "ReviewCollectionCellPage", for: indexPath) as! ReviewCollectionCellPage
            cell.finalImage.image = UIImage(named: "images.png")
        collectionView.reloadData()
        return cell
    }

        func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
            return CGSize(width: 50, height: 50)
        }

        func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
            return UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
        }
}

Solution

  • The code runs into an infinite loop.

    In cellForRow calling reloadData calls cellForRow which calls reloadData which calls cellForRow which calls reloadData which calls cellForRow which calls reloadData which calls cellForRow which calls reloadData which calls cellForRow which calls reloadData which calls cellForRow... which 🧨 with an overflow exception

    Delete collectionView.reloadData() in cellForRow and reload the data right after populating the data source array.