2015-08-12 39 views
9

Ho bisogno di aggiungere intestazione personalizzata al mio tavoloSwift - come rendere intestazione personalizzata per UITableView?

provo questo

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 

    let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 18)) 
    let label = UILabel(frame: CGRect(x: 20, y: 20, width: 50, height: 50)) 
    label.text = "TEST TEXT" 
    label.textColor = UIColor.whiteColor() 

    self.view.addSubview(view) 

    return view 
} 

ma questo non funziona, non vedo nulla sul tavolo

Che cosa sto facendo di sbagliato? O forse ci sono altri modi?

+3

Aggiunta 'view' a' self.view' non è necessario. Sarà rimosso da lì e aggiunto alla vista tabella. – Cyrille

+0

@Cyrille Non sono sicuro del motivo per cui pensi che la vista verrebbe rimossa dopo che è stata aggiunta a livello di codice alla matrice di sottoview, ma mi sembra che sarebbe semplicemente aggiunta alla gerarchia della vista in due punti diversi, il che porterebbe a un indefinito comportamento. – jlehr

+0

No. Una vista può avere solo una superview alla volta.Se aggiungi una vista ovunque, verrà prima rimossa da dove era precedentemente. – Cyrille

risposta

12

Si è impostata la sezione di altezza intestazione nel viewDidLoad?

self.tableView.sectionHeaderHeight = 70 

Inoltre è necessario sostituire

self.view.addSubview(view) 

da

view.addSubview(label) 

Finalmente hai per controllare i fotogrammi

let view = UIView(frame: CGRect.zeroRect) 

e, infine, il colore del testo desiderato come sembra essere attualmente bianco su bianco.

+0

Grazie mille! Soprattutto per le osservazioni su ViewDidLoad. Ora funziona alla grande –

7

aggiungere label a subview di costume view, non c'è bisogno di self.view.addSubview(view), perché viewForHeaderInSection ritorno UIView

view.addSubview(label) 
+0

È necessario aggiungere l'etichetta, sì; ma l'aggiunta di 'view' a' self.view' non è necessaria. Sarà rimosso da lì e aggiunto alla vista tabella. – Cyrille

4

Se si utilizza cella personalizzata come intestazione, aggiungere quanto segue.

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 

     let headerView = UIView() 
     let headerCell = tableView.dequeueReusableCell(withIdentifier: "customTableCell") as! CustomTableCell 
     headerView.addSubview(headerCell) 
     return headerView 
    } 

Se si desidera avere una vista semplice, aggiungere quanto segue.

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
let headerView:UIView = UIView() 
    return headerView 
} 
4

Se siete disposti ad utilizzare intestazione tabella personalizzata come intestazione della tabella, provare le seguenti ....

Aggiornato per SWIFT 3.0

Fase 1

Crea UITableViewHeaderFooterView per intestazione personalizzata ..

import UIKit 

class MapTableHeaderView: UITableViewHeaderFooterView { 

    @IBOutlet weak var testView: UIView! 

} 

Fase 2

Aggiungi intestazione personalizzata per UITableView

override func viewDidLoad() { 
      super.viewDidLoad() 

      tableView.delegate = self 
      tableView.dataSource = self 

      //register the header view 

      let nibName = UINib(nibName: "CustomHeaderView", bundle: nil) 
      self.tableView.register(nibName, forHeaderFooterViewReuseIdentifier: "CustomHeaderView") 


    } 

    extension BranchViewController : UITableViewDelegate{ 

    } 

    extension BranchViewController : UITableViewDataSource{ 

     func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
      return 200 
     } 

     func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
      let headerView = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: "CustomHeaderView") as! MapTableHeaderView 

      return headerView 
     } 

     func tableView(_ tableView: UITableView, numberOfRowsInSection section: 

    Int) -> Int { 
      // retuen no of rows in sections 
     } 

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
      // retuen your custom cells  
     } 

     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

     } 

     func numberOfSections(in tableView: UITableView) -> Int { 
      // retuen no of sections 
     } 

     func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
      // retuen height of row 
     } 


    } 
0

Questo ha funzionato per me - Swift 3

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 

     let headerCell = tableView.dequeueReusableCell(withIdentifier: "customTableCell") as! CustomTableCell 
     return headerCell 
    } 
Problemi correlati