Почему мой CollectionViewController не отображается? - PullRequest
1 голос
/ 17 января 2020

У меня есть проект с CollectionViewController, я сделал это программно, я изменил цвет фона внутри CollectionViewController, но по какой-то причине, когда я запускаю приложение, изменений нет, мой экран все еще черный, можете ли вы мне помочь?

вот мой код:

The CollectionViewController class code:
//
//  NearCollectionVC.swift
//  xCodeMaps
//
//  Created by Cristea Octavian on 16/01/2020.
//  Copyright © 2020 Cristea Octavian. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class NearCollectionVC: UICollectionViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    print("-----SETTING-----")

    collectionView!.backgroundColor = UIColor.red

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Register cell classes
    self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

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

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

// MARK: UICollectionViewDataSource

override func numberOfSections(in collectionView: UICollectionView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}


override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of items
    return 0
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)

    // Configure the cell

    return cell
}

}

Также я должен отметить, что весь код внутри моего CollectionViewController запущен, я вижу напечатанный на моей консоли "----- SETTING-- --- "но цвет моего фона не меняется

И код, в котором я настраиваю класс

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        window = UIWindow()
           window?.makeKeyAndVisible()

           let layout = UICollectionViewFlowLayout()
           let cvc = NearCollectionVC(collectionViewLayout: layout)

           window?.rootViewController = cvc
        // Override point for customization after application launch.
        return true
    }

Я заглянул внутрь иерархии отладочных представлений и, похоже, мой CollectionViewController не виден

enter image description here

Ответы [ 2 ]

0 голосов
/ 20 марта 2020

У вас нет отображаемых ячеек, увеличьте число в функции noItemsInSection до значения, отличного от 0. Кроме того, расширьте протокол UICollectionViewDelegateFlowLayout, чтобы установить высоту и ширину ячейки. Как это

 class NearCollectionVC: UICollectionViewController, UICollectionViewDelegateFlowLayout {
   //Set the height like this
   func collectionView(_: UICollectionView, layout _: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

                return CGSize(width: 100, height: 100)
        }
    }
     }
0 голосов
/ 18 января 2020

Вы не использовали раскадровку и SceneDelegate. сначала необходимо удалить Main interface изображение , затем вам необходимо удалить эти ключи в info.plist. изображении

...