0

I have a view controller with two collection views and two view controllers in a view that is inside of a scrollview in order to see other views that I haven't added yet. I had all these objects outside of a scrollview and when I added a scroll view and inserted the objects I get a the code:

Thread 1: "*** -[__NSArrayM objectAtIndexedSubscript:]: index 1 beyond bounds [0 .. 0]"

when I launch the app. I am able to run the app and when the launch screen is done loading, it crashes.

Here is the code:

import UIKit

class HomeViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

@IBOutlet weak var t10MacInfCollectionView: UICollectionView!

@IBOutlet weak var t10MicroInfCollectionView: UICollectionView!

let T10Mac = "T10Mac"

let T10Mic = "T10Mic"

override func viewDidLoad() {
    super.viewDidLoad()

    t10MacInfCollectionView.reloadData()
    t10MicroInfCollectionView.reloadData()
    
    t10MacInfCollectionView.delegate = self
    t10MicroInfCollectionView.delegate = self

    t10MacInfCollectionView.dataSource = self
    t10MicroInfCollectionView.dataSource = self

    self.view.addSubview(t10MacInfCollectionView)
    
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    
    if collectionView == self.t10MacInfCollectionView {
    
    return 10
    }
    
    return 10
    
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  
    if collectionView == self.t10MacInfCollectionView {
    
    let cellA = collectionView.dequeueReusableCell(withReuseIdentifier: T10Mac , for: indexPath) as! T10MacroCollectionViewCell
    
    return cellA
    }
    
    else {
        
    let cellB = collectionView.dequeueReusableCell(withReuseIdentifier: T10Mic , for: indexPath) as! T10MicroCollectionViewCell
    
    return cellB
    }
    
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    if collectionView == self.t10MacInfCollectionView {
   
           let cellA = collectionView.dequeueReusableCell(withReuseIdentifier: T10Mac, for: indexPath)
           
        return CGSize(width: 125.0, height: 225.0)
       }

       else {
           let cellB = collectionView.dequeueReusableCell(withReuseIdentifier: T10Mic, for: indexPath)
       
        return CGSize(width: 125.0, height: 225.0)
           }
 }

}

If there is some other code that you guys need to see please let me know. I don't understand why I am getting this error.

Edit: Here is what my exception breakpoint is saying:

[CollectionView] An attempt to update layout information was detected while already in the process of computing the layout (i.e. reentrant call). This will result in unexpected behaviour or a crash. This may happen if a layout pass is triggered while calling out to a delegate. UICollectionViewFlowLayout instance is (<UICollectionViewFlowLayout: 0x7f986fc357c0>)
NewCoder
  • 35
  • 6
  • On what line do you get the crash? You can use an exception breakpoint in Xcode to narrow it down. – koen Sep 28 '20 at 17:03
  • Hey Koen. Thank you for the fast response. It's happening in my app delegate on the line: class AppDelegate: UIResponder, UIApplicationDelegate {. Where should I be putting my breakpoint? – NewCoder Sep 28 '20 at 17:08
  • Without analysing the code to much but maybe you should set the delegate and datasource first _then_ call `reloadData()` – Joakim Danielson Sep 28 '20 at 17:08
  • @NewCoder: https://stackoverflow.com/questions/17802662/how-to-add-exception-breakpoint-in-xcode – koen Sep 28 '20 at 17:11
  • Hey Joakim. Thank you for your advice. Unfortunately I tried that and I even added a superview that was missing and it still didn't work. – NewCoder Sep 28 '20 at 17:11
  • Hey Koen. Where should I put the breakpoint since it's crashing at the app delegate? – NewCoder Sep 28 '20 at 17:12
  • Hey Koen. I think I added a screenshot of the thing you're asking for under an edit on the question. – NewCoder Sep 28 '20 at 17:20
  • Hey Koen. I apologize but my debug area or whatever it's called doesn't have those buttons. That question is from 2014 and my Xcode is the most up to date. How can I add the exception breakpoint? – NewCoder Sep 28 '20 at 17:48

0 Answers0