ios - Collection View /选项卡栏 Controller 崩溃

标签 ios xcode uicollectionview uitabbarcontroller

在我的应用程序中,我使用标签栏 Controller 在 View Controller 之间切换,并且在每个 View Controller 上我使用 Collection View 然后转到不同的页面。当我在计算机上运行该应用程序时,一切正常。问题是当我在手机上运行该应用程序时。每当我单击选项卡栏 Controller 中的选项卡,该选项卡转到上面带有 Collection View Controller 的页面时,应用程序就会卡住然后崩溃。我认为我已经将问题缩小到 Collection View Controller ,因为当没有 Collection View Controller 时,我手机上的应用程序不会崩溃。它仅在使用 Collection View Controller 时崩溃。这可能是什么原因造成的?我已经看过,但没有发现任何类似的问题。

这是其中一个 Collection View Controller 的代码:

import UIKit
class HighSchoolController: UIViewController, \UICollectionViewDataSource, UICollectionViewDelegate {

    var identities = [String]()

    let hsImage: [UIImage] = [
        UIImage(named: "dates")!,
        UIImage(named: "athletics_icon")!,
        UIImage(named: "interm-1")!,
        UIImage(named: "lunch_icon")!,
        UIImage(named: "pioneerpress")!,
        UIImage(named: "PIONEERNEWS")!,
        UIImage(named: "clubs_icon")!,
        UIImage(named: "contact_icon")!
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

        identities = ["events", "athletics", "da", "lunch", "pioneer press", "pioneer news", "clubs", "contact"]

        //Navigationbar Shadow
        self.navigationController?.navigationBar.layer.shadowColor = UIColor.black.cgColor
        self.navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 0.0, height: 4.0)
        self.navigationController?.navigationBar.layer.shadowRadius = 9.0
        self.navigationController?.navigationBar.layer.shadowOpacity = 0.9
        self.navigationController?.navigationBar.layer.masksToBounds = false

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

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

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


        hsCell.hsImage.image = hsImage[indexPath.item]

        hsCell.layer.cornerRadius = (hsCell.layer.frame.height) / 2.0;
        hsCell.layer.masksToBounds = true

        return hsCell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let vcName = identities[indexPath.row]
        let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
        self.navigationController?.pushViewController(viewController!, animated: true)
    }
}

最佳答案

我通过在我的标签栏 Controller 中这样做来解决这个问题

  • 让 layout1 = UICollectionViewFlowLayout() 让 cv1 = CollectionView1(collectionViewLayout: layout1)

  • 让 layout2 = UICollectionViewFlowLayout() let cv2 = CollectionView1(collectionViewLayout: layout2)

  • 让 layout3 = UICollectionViewFlowLayout() let cv3 = CollectionView1(collectionViewLayout: layout3)

  • 让 layout4 = UICollectionViewFlowLayout() let cv4 = CollectionView1(collectionViewLayout: layout4)

关于ios - Collection View /选项卡栏 Controller 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53012607/

相关文章:

ios - 收藏 View 图像未显示,但 Url 工作正常

ios - 删除 iOS 7 状态栏

iphone - iOS 多客户端部署/分发问题

Swift 控制流(函数调用)

ios - 如何在 swift 中添加新键但不向字典添加值?

ios - UICollectionView 总是落后一项

ios - UICollectionView 单元格对齐方式

javascript - 更新后在 Jquerymobile 中刷新 Gridview

ios - 将 UIImageView 放置在另一个具有缩放功能的上方

objective-c - 解析 CSV : how can NSScanner recognize empty field (i. e。 ,,)?