错误:线程1:“ ***-[__ NSArrayM objectAtIndexedSubscript:]:索引1超出范围[0 .. 0]”

时间:2020-09-28 16:55:37

标签: swift xcode uicollectionview crash

在滚动视图内部的视图中,我有一个具有两个集合视图和两个视图控制器的视图控制器,以查看尚未添加的其他视图。我所有这些对象都在滚动视图之外,当我添加滚动视图并插入对象时,我得到了代码:

线程1:“ ***-[__ NSArrayM objectAtIndexedSubscript:]:索引1超出 界限[0 .. 0]“

启动应用程序时。我能够运行该应用程序,并且在完成启动屏幕的加载后,它会崩溃。

代码如下:

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)
           }
 }

}

如果还有其他需要查看的代码,请告诉我。我不明白为什么会收到此错误。

编辑:这是我的异常断点在说什么:

[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>)

0 个答案:

没有答案