2017-06-03 13 views
0

こんにちは私はプログラムでcollectionViewを作成しようとしています。私はその色を変更しましたが、それはまだデフォルトのものを示しています。コードは実際には機能しません。私はappDelegateでの問題はかなり確信していますが、それを把握することはできません。ここ はappDelegateで自分のアプリケーションのメソッドです:CollectionViewは黒い画面を表示します

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    // Create one 
    window = UIWindow(frame: UIScreen.main.bounds) 

    //Shows the window and makes it the key window. 
    window?.makeKeyAndVisible() 

    //Must give a layout to it by default 
    let flowLayout = UICollectionViewFlowLayout() 
    let customCollectionViewController = UICollectionViewController(collectionViewLayout: flowLayout) 
    window?.rootViewController = UINavigationController(rootViewController: customCollectionViewController) 
    return true 
} 

、ここではCollectionViewControllerコード

class CustomCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 

let identifier = "customCell" 
override func viewDidLoad() { 
    super.viewDidLoad() 
    collectionView?.backgroundColor = UIColor.gray 
    collectionView?.register(CustomCell.self, forCellWithReuseIdentifier: identifier) 
} 


override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let customCell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) 
    return customCell 
} 



override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 3 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    return CGSize(width: view.frame.width, height: 100) 
} 

}

class CustomCell: UICollectionViewCell{ 
    override init(frame: CGRect) { 
    super.init(frame: frame) 
    setupViews() 
} 

let nameLabel: UILabel = { 
    let label = UILabel() 
    label.text = "Hello World" 

    label.translatesAutoresizingMaskIntoConstraints = false 
    return label 

}() 

func setupViews(){ 
    backgroundColor = UIColor.white 
    addSubview(nameLabel) 

    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|v0|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel])) 
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|v0|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel])) 
} 

required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
} 

}

である私はXcodeの8.2.1

を使用しています
+0

コレクションビューを含むコントローラが初期ビューコントローラとして設定されていますか? –

答えて

0

CustomCollectionViewControllerを使用していない、UICollectionViewControllerを使用しています。

も私のためにクラッシュを引き起こしている細胞内の

let customCollectionViewController = CustomCollectionViewController(collectionViewLayout: flowLayout) 

あなたの制約コードを

let customCollectionViewController = UICollectionViewController(collectionViewLayout: flowLayout) 

を交換していますが、単にこれらの行をコメントする場合は、collectionviewが表示されるはずです。

関連する問題