2017-09-19 1 views

Répondre

0

Après, substituez la fonction détermine indexPath.section et retourner le titre.

func tableView(tableView: UITableView!, titleForHeaderInSection section: Int) -> String! 

Et, vous devez faire UIColectionView comme sous-vue UITableViewCell, Tous la configuration UICollectionViewCell sera géré dans le UITableViewCell

1

Supposons que vous ayez XIB avec étiquette et que vous voulez montrer en-tête de vue collection

premier registre XIB dans viewDidLoad

utilisant

open func register(_ viewClass: Swift.AnyClass?, forSupplementaryViewOfKind elementKind: String, withReuseIdentifier identifier: String) 

i.e.

collectionView.register(UINib.init(nibName: "NIB", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "Header") 

Après cela, vous pouvez configurer votre point de vue de la collecte de section comme celui-ci

extension ViewController :UICollectionViewDelegate,UICollectionViewDataSource { 
    func numberOfSections(in collectionView: UICollectionView) -> Int { 
     return 5; 
    } 
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     return UICollectionViewCell() 
    } 

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

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 
     if kind.isEqual(UICollectionElementKindSectionHeader) { 
      let cell = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Header", for: indexPath) as! MySectionHeaderClass 
      cell.title = "MyString " 
      return cell 
     } else { 
      return nil 
     } 
    } 
} 

//MAKE YOUR Collection view item width to full screen 

espère qu'il est clair pour vous

0

Cela a fonctionné pour moi.

// in viewDidload 

self.UICollectionView.register(UICollectionViewCell.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerId") 

// use delegate delegate   
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 
     let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerId, for: indexPath) 
     var lblHeaher : UILabel! 

    // as header is dequing that's why we need to check if label is there then no need to init label again and again 
    if header.viewWithTag(101) == nil { 
     lblHeaher = UILabel.init(frame: CGRect(x: 30, y: 0, width: servicesCollectionView.frame.width, height: 40)) 
     lblHeaher.tag = 101 
    }else{ 
     lblHeaher = header.viewWithTag(101) as! UILabel 
    } 


    lblHeaher.text = "category \(indexPath.row)" 
    lblHeaher.textAlignment = .left 
    lblHeaher.textColor = UIColor.white 
    lblHeaher.font = UIFont.init(name: "ProximaNova-Semibold", size: 20)! 

    header.backgroundColor = .lightGray 
    header.addSubview(lblHeaher) 
    return header 
} 
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { 
    return CGSize(width: self.frame.width, height: 40) 
} 

func numberOfSections(in collectionView: UICollectionView) -> Int { 
    return numberofSection 
} 
+0

Merci pour votre aide. –