无法将集合视图添加到两个视图控制器

时间:2019-07-04 02:43:20

标签: ios swift uicollectionview

我已经将集合视图添加到两个视图控制器中,并且当我单击第一个视图控制器(TabBar.swift)中的单元格时,它应该转到第二个视图控制器,反之亦然,但是由于某种原因,启动时应用崩溃。

这是第一个视图控制器的代码: TabBar.swift

struct OpenViewController {
var viewController: UIViewController

}
class TabBar: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white
        addCollectionViewConstraints()
    }

    var viewControllerList = [
    OpenViewController(viewController: FirstScreen())
    ]

    // Setup the collection veiw

    let collectionView: UICollectionView = {

        let layout = UICollectionViewFlowLayout()
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellID")
        cv.translatesAutoresizingMaskIntoConstraints = false
        return cv
    }()

    // Add constraints to the collection view
    func addCollectionViewConstraints() {

        view.addSubview(collectionView)
        collectionView.backgroundColor = .blue
        collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 10).isActive = true
        collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        collectionView.heightAnchor.constraint(equalTo: collectionView.widthAnchor, multiplier: 0.3).isActive = true

        collectionView.delegate = self
        collectionView.dataSource = self
    }

}

这是第二个视图控制器的代码: FirstScreen.swift

struct OpenViewControllerTwo {
    var viewController: UIViewController
}
class FirstScreen: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .yellow
        addCollectionViewConstraints()

    }

    var viewControllerList = [
        OpenViewControllerTwo(viewController: FirstScreen())
    ]

    let collectionView: UICollectionView = {

        let layout = UICollectionViewFlowLayout()
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellID")
        cv.translatesAutoresizingMaskIntoConstraints = false
        return cv
    }()

    // Add constraints to the collection view
    func addCollectionViewConstraints() {

        view.addSubview(collectionView)
        collectionView.backgroundColor = .blue
        collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 10).isActive = true
        collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        collectionView.heightAnchor.constraint(equalTo: collectionView.widthAnchor, multiplier: 0.3).isActive = true

        collectionView.delegate = self
        collectionView.dataSource = self
    }
}

extension FirstScreen: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

    // The number of elements inside the collection view
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }

    // Adding the cells
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellID", for: indexPath)

        // Customize the cells
        cell.backgroundColor = .black
        cell.layer.cornerRadius = collectionView.contentSize.height / 2
        cell.layer.borderWidth = 1
        cell.layer.borderColor = UIColor.black.cgColor

        return cell
    }

    // The witdth and the height of the cell
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.frame.height, height: collectionView.frame.height)
    }

    // Padding to the cells
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)
    }

    // Selecting a cell and navigating to another view controller
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        let openViewController = self.viewControllerList[indexPath.row]
        self.present(openViewController.viewController, animated: false)

    }

}

崩溃日志:

Crash log

2 个答案:

答案 0 :(得分:1)

由于viewControllerList是在循环中初始化的,因此您正在获取内存问题。 像这样声明变量

rm -fr ~/.gradle/caches/

然后在viewDidLoad中分配这样的值。

var viewControllerList = [OpenViewController]()

答案 1 :(得分:0)

我在FirstScreen中找到了该代码

var viewControllerList = [
    OpenViewControllerTwo(viewController: FirstScreen())
]

在FirstScreen初始化时,viewControllerList会全部初始化FirstScreen。 它创建了递归递归。请更改上面的代码,放入另一个类,例如:在FirstClass

var viewControllerList = [
    OpenViewControllerTwo(viewController: SecondClass())
]