2017-01-05 4 views
3

En rapide 3 avec Xcode 8.2 Je ne peux pas obtenir un reusableHeaderView pour afficher avec UICollectionView. J'ai placé des points d'arrêt à divers points dans mon fichier de vue d'en-tête et ils ne sont jamais déclenchés. Voici mon code.Swift 3 UICollectionView tête de section ne sera pas réutilisable afficher

import UIKit 

class WishListViewController: UIViewController, 
         UICollectionViewDelegateFlowLayout, 
         UICollectionViewDataSource { 

var collectionView: UICollectionView! 

var wishListItems = [[String:Any]]() 

var shops = [[String: Any]]() 

let cellId = "WishListCell" 

let headerId = "WishListHeaderView" 

var screenSize: CGRect! 
var screenWidth: CGFloat! 
var screenHeight: CGFloat! 

let imageAspectRatio: CGFloat = 1.2 

override func viewDidLoad() { 

    super.viewDidLoad() 

    screenSize = UIScreen.main.bounds 
    screenWidth = screenSize.width 
    screenHeight = screenSize.height 

    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 
    layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) 
    layout.itemSize = CGSize(width: screenWidth, height: ((screenWidth/3) * imageAspectRatio) + 20) 
    layout.minimumInteritemSpacing = 0 
    layout.minimumLineSpacing = 0 
    layout.estimatedItemSize.height = ((screenWidth/3) * imageAspectRatio) + 20 
    layout.estimatedItemSize.width = screenWidth 

    collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout) 

    collectionView.delegate = self 

    collectionView.dataSource = self 

    collectionView.register(WishListCell.self, forCellWithReuseIdentifier: cellId) 

    collectionView.register(WishListHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerId) 

    collectionView.backgroundColor = UIColor.white 

    self.view.addSubview(collectionView) 

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

func collectionView(_ collectionView: UICollectionView, 
        viewForSupplementaryElementOfKind kind: String, 
        at indexPath: IndexPath) -> UICollectionReusableView { 

    switch kind { 

    case UICollectionElementKindSectionHeader: 

     let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, 
                     withReuseIdentifier: headerId, 
                     for: indexPath) as! WishListHeaderView 

     headerView.textLabel.text = shops[indexPath.section]["shop_name"] as? String 

     return headerView 

    default: 

     assert(false, "Unexpected element kind") 
    } 
} 

Voici mon fichier de vue d'en-tête.

import UIKit 

class WishListHeaderView: UICollectionReusableView { 

var textLabel: UILabel 

let screenWidth = UIScreen.main.bounds.width 

override init(frame: CGRect) { 

    textLabel = UILabel() 

    super.init(frame: frame) 

    self.addSubview(textLabel) 

    textLabel.font = UIFont.systemFont(ofSize: UIFont.smallSystemFontSize) 

    textLabel.textAlignment = .left 

    textLabel.numberOfLines = 0 

    textLabel.lineBreakMode = NSLineBreakMode.byWordWrapping 

    textLabel.translatesAutoresizingMaskIntoConstraints = false 

    NSLayoutConstraint.activate([ 


     NSLayoutConstraint(item: textLabel, attribute: .leading, relatedBy: .equal, 
          toItem: self, attribute: .leadingMargin, 
          multiplier: 1.0, constant: 0.0), 

     NSLayoutConstraint(item: textLabel, attribute: .trailing, relatedBy: .equal, 
          toItem: self, attribute: .trailingMargin, 
          multiplier: 1.0, constant: 0.0), 
     ]) 

    self.backgroundColor = .white 

} 

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

Répondre

12

Vous devez implémenter la hauteur pour la fonction d'en-tête pour votre vue de collection.

func collectionView(_ collectionView: UICollectionView, 
       layout collectionViewLayout: UICollectionViewLayout, 
       referenceSizeForHeaderInSection section: Int) -> CGSize 

puis retournez la hauteur souhaitée.