2017-11-09 4 views
1

Ich möchte ein Label zu meiner UICollectionView programmgesteuert hinzufügen, und habe die ViewForSupplementaryElementOfKind und ReferenceSizeForHeaderInSection verwendet, um es einzurichten, aber aus irgendeinem Grund, wenn ich meine Ansichten einrichten es noch in der ersten Zeile von platziert meine CollectionView anstelle der erstellten Kopfzeile. As you can see in this screenshot, "Today" is in the first cell, instead of the header I created for itSo fügen Sie UICollectionView Header hinzu

class TvController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 

private let cellId = "cellId" 
private let headerId = "headerId" 

override func viewDidLoad() { 
    super.viewDidLoad() 

    navigationItem.title = "TV" 
    navigationController?.navigationBar.isTranslucent = false 

    collectionView?.backgroundColor = .white 

    collectionView?.register(TvCell.self, forCellWithReuseIdentifier: cellId) 
    collectionView?.register(Header.self, forCellWithReuseIdentifier: headerId) 

} 

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

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

    return cell 
} 

//Row for each TV show 
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    return CGSize(width: view.frame.width, height: 120) 
} 

//Today's date header 
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 
    let header = collectionView.dequeueReusableCell(withReuseIdentifier: headerId, for: indexPath) as! Header 

    return header 
} 

//Today's date header 
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { 
    return CGSize(width: view.frame.width, height: 60) 
} 
} 

Hier ist die Header-Klasse

class Header: UICollectionViewCell { 

override init(frame: CGRect) { 
    super.init(frame: frame) 
    setupHeaderViews() 
} 

let dateLabel: UILabel = { 
    let title = UILabel() 
    title.text = "Today" 
    title.textColor = .gray 
    title.backgroundColor = .black 
    title.font = UIFont(name: "Montserrat", size: 17) 
    title.translatesAutoresizingMaskIntoConstraints = false 
    return title 
}() 

func setupHeaderViews() { 
    addSubview(dateLabel) 

    dateLabel.leftAnchor.constraint(equalTo: leftAnchor, constant: 20).isActive = true 
    dateLabel.topAnchor.constraint(equalTo: topAnchor, constant: 10).isActive = true 
    dateLabel.widthAnchor.constraint(equalToConstant: 120).isActive = true 
    dateLabel.heightAnchor.constraint(equalToConstant: 30).isActive = true 
} 


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

Antwort

0

Sie die falsche Methode mit Ihrem Header zu registrieren. Sie werden auch mit der falschen Methode aus der Warteschlange genommen. Da Sie Ihre Header als forSupplementaryViewOfKind sind Registrierung - Sie haben deque den Header zu verwenden, mit Methode ‚dequeueReusableSupplementaryView‘ statt ‚dequeueReusableCell‘

override func viewDidLoad() { 
    collectionView?.register(Header.self, forSupplementaryViewOfKind: 
     UICollectionElementKindSectionHeader, withReuseIdentifier: headerId) 
    } 


override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: 
    String, at indexPath: IndexPath) -> UICollectionReusableView { 
     let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: 
      headerId, for: indexPath) as! Header 
     return header 
} 
+0

Vielen Dank für Ihre Antwort antwash, wie es scheint jetzt bekomme ich einen Fehler beim Build. Es heißt: Beenden App aufgrund der nicht abgefangenen Ausnahme 'NSInternalInconsistencyException', Grund: 'konnte eine Ansicht von Art nicht entfernen: UICollectionElementKindCell mit Kennung HeaderId - muss eine Spitze oder eine Klasse für den Bezeichner registrieren oder eine Prototypzelle in einem Storyboard verbinden' Wissen Sie, was ich jetzt tun müsste? –

+0

Sehen Sie sich die Änderung an, sie zeigt, was Sie in den Methoden viewDidLoad und viewForSupplementaryElementOfKind haben sollten. Hoffe du verstehst :) – antwash

+0

Ahhh Ich sehe, tut mir leid, ich lerne immer noch :) Vielen Dank für Ihre Hilfe! Ich verstehe jetzt. –

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

      let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HeaderId", for: indexPath) as! HeaderClassname 
      switch kind { 

      case UICollectionElementKindSectionHeader: 



       if indexPath.section == 0 
       { 
        Youre Code Here 


       } 
       else 
       { 



        } 
        return headerView 

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

      return headerView 
     } 
Verwandte Themen