1

Je travaillais sur une application iOS et j'ai plusieurs problèmes concernant l'utilisation de la cellule UICollectionView.Afficher l'en-tête de section UICollectionReusableView

Cette fois-ci, je veux poser des questions sur la façon d'afficher l'en-tête de la section de UICollectionView (UICollectionReusableView)

je mets en œuvre déjà la fonction comme ci-dessous:

public func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { 

     switch kind { 

     case UICollectionElementKindSectionHeader: 

      let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "cellHeader", for: indexPath as IndexPath) 
      var labelHeader = headerView.viewWithTag(2) as! UILabel 

      if indexPath.section == 0 { 
       labelHeader.text = "Specialist Clinic" 

      } 
      else { 
       labelHeader.text = "Medical Support" 
      } 

      headerView.backgroundColor = UIColor.blue; 
      return headerView 

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

mais il donne toujours un résultat blanc . s'il vous plaît regarder la capture d'écran ci-dessous

Section Header did not show anything (section 1)

section 2

+0

Avez-vous essayé l'application de la méthode déléguée vue réutilisable hauteur? – danieltmbr

Répondre

2

Vous devez retourner la taille d'en-tête.

func collectionView(_ collectionView: UICollectionView, 
        layout collectionViewLayout: UICollectionViewLayout, 
        referenceSizeForHeaderInSection section: Int) -> CGSize{ 
     return CGSize(width: CGFloat(collectionView.frame.size.width, height: CGFloat(135)) // you can change here 
    } 

méthode déléguée

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 
      var reusableview: UICollectionReusableView? = nil 
      if kind == UICollectionElementKindSectionHeader { 
       reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "cellHeader", for: indexPath) // cellHea is your identifier 
      var labelHeader = reusableview.viewWithTag(2) as! UILabel 

     if indexPath.section == 0 { 
       labelHeader.text = "Specialist Clinic" 

      } 
      else { 
       labelHeader.text = "Medical Support" 
      } 

      headerView.backgroundColor = UIColor.blue; 

      } 
      return reusableview! 
     } 
0

Vous devez ajouter UILabel sur headerView

 public func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { 

    switch kind { 

    case UICollectionElementKindSectionHeader: 

     let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "cellHeader", for: indexPath as IndexPath) 
     var labelHeader = headerView.viewWithTag(2) as! UILabel 

     if indexPath.section == 0 { 
      labelHeader.text = "Specialist Clinic" 

     } 
     else { 
      labelHeader.text = "Medical Support" 
     } 

     headerView.backgroundColor = UIColor.blue; 
     headerView.addSubview(labelHeader) //Add UILabel on HeaderView 
     return headerView 

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