2017-05-16 10 views
1

私はヘッダーを持つUICollectionViewを作成したいと思います。 mainStoryBoardでCollection Reusable Viewを設定しましたが、デバイスには何も表示されません。私は検索しようとしたが、なぜそれが出現していないのか分からなかった。デバイス上でIコレクションの再利用可能なビューが表示されない理由

メインストーリーボード

enter image description here

enter image description here

輸入のUIKit

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { 


    @IBOutlet weak var collectionView: UICollectionView! 

    var images = ["medal1","medal2","medal3","medal4","medal5","medal6","medal7","medal8","medal9","medal10","medal1","medal2","medal3","medal14"] 

    var texts = ["hi","yes","hoo","such","hi","yes","hoo","such","hi","yes","hoo","such","hi","yes"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 



     collectionView.delegate = self 
     collectionView.dataSource = self 

    } 

    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

     return images.count 
    } 

    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell 

     cell.myImage.image = UIImage(named: images[indexPath.row]) 

     cell.achievementLabel.text = texts[indexPath.row] 

     return cell 

    } 

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
     return 1 
    } 






} 


import UIKit 

コレクションビュー クラスCustomCellのためのクラス: UICollectionViewCellコレクション再利用可能なビューの{

@IBOutlet weak var myImage: UIImageView! 

    @IBOutlet weak var achievementLabel: UILabel! 

} 

クラス 輸入のUIKit

class CollectionReusableView: UICollectionReusableView { 

    @IBOutlet weak var reuseableVimage: UIImageView! 
} 


> import UIKit 

class ViewController: UIViewController, UICollectionViewDelegate { 


    @IBOutlet weak var collectionView: UICollectionView! 

    var images = ["medal1","medal2","medal3","medal4","medal5","medal6","medal7","medal8","medal9","medal10","medal1","medal2","medal3","medal14"] 

    var texts = ["hi","yes","hoo","such","hi","yes","hoo","such","hi","yes","hoo","such","hi","yes"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 



     collectionView.delegate = self 


    } 

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 
     if kind == UICollectionElementKindSectionHeader { 
      let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HeaderView", for: indexPath) 
      // do any programmatic customization, if any, here 
      return view 
     } 
     fatalError("Unexpected kind") 








} 
} 

答えて

6

あなたはviewForSupplementaryElementOfKindを実装する必要があります。

  1. 適宜、UICollectionElementKindSectionHeaderまたはUICollectionElementKindSectionFooterため、collectionView(_:viewForSupplementaryElementOfKind:at:)を実装します。

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 
        if kind == UICollectionElementKindSectionHeader { 
         let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "CollectionReusableView", for: indexPath) as! CollectionReusableView 
         // do any programmatic customization, if any, here 
         return view 
        } 
        fatalError("Unexpected kind") 
    } 
    
  2. IBにおけるヘッダの再利用可能なビューが

    • 適切な基底クラスを有することを確認します。そして
    • 適切な「リユース識別子」
    • IBで
  3. 、必要に応じて、コレクションビューの「アクセサリ」、「セクションヘッダ」と「セクションフッター」の横にチェックマークがあることを確認してください。

+0

ありがとうございました。私はコードを更新しましたが、画面には何も表示されません。私はIBとアクセサリーをチェックしました。 – rebecca87

+0

警告がないと仮定すると、ビューデバッガをチェックし、ビューがコレクションビュー内にあり、そのフレームが何であるかを確認することがあります。実際にビューがない場合は、 'UICollectionViewDataSource'メソッドにブレークポイントを設定し、それらが呼び出され、返される値を確認します。 – Rob

+0

collectionView.dataSource = selfを設定すると、クラッシュする。 – rebecca87

1

はこれを実装するようにしてください。あなたのために有用である可能性RayWenderlichの例があります:https://www.raywenderlich.com/136161/uicollectionview-tutorial-reusable-views-selection-reordering

override func collectionView(_ collectionView: UICollectionView, 
          viewForSupplementaryElementOfKind kind: String, 
          at indexPath: IndexPath) -> UICollectionReusableView { 
switch kind { 
case UICollectionElementKindSectionHeader: 
    let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, 
                      withReuseIdentifier: "CollectionReusableView", 
                      for: indexPath) as! CollectionReusableView 
    headerView.reuseableVimage .... 
    return headerView 
default: 
    assert(false, "Unexpected element kind") 
} 
} 
+0

おかげで、実は私はこのコードを実装するためのウェブサイトを見ました。.. – rebecca87

+0

それは迅速3でいくつかの変更があるようです:/ – rebecca87

関連する問題