2015-07-01 12 views
6

Sto provando a creare una tabella in cui le intestazioni di sezione possono essere stringhe lunghe. Pensavo di avere le impostazioni giuste (numero dinamico di linee, set di word wrapping) ma invece la stringa è semplicemente troncata alla fine. Si noti che l'intestazione della sezione è dimensionata con un'altezza di 80, altrove, che è sufficiente per visualizzare circa 3 righe di testo.Intestazione della sezione tabella: multi-linea/word wrapping

// Format section header 
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { 

    let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView 
    header.contentView.backgroundColor = mainColorBlue 
    header.textLabel.textColor = UIColor.whiteColor() 

    header.textLabel.textAlignment = NSTextAlignment.Left 
    header.textLabel.numberOfLines = 0 // Dynamic number of lines 
    header.textLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping 
    header.textLabel.font = UIFont(name: "HelveticaNeue-Thin", size: 16)! 
    header.textLabel.text = objectsArray[section].sectionName 

} 
+0

Hai funzionato? Apprezzerei sapere come l'hai risolto. – zeeple

+0

Sto ancora avendo esattamente lo stesso problema – SwiftMatt

+0

Qualcuno ha una soluzione per questo? – po5i

risposta

0

Faccio il contrario, ho impostato il numero di linee su un numero enorme e quindi la dimensione dinamica funziona.

  • In IB
  • Non impostare qualsiasi larghezza/altezza costanti
  • solo leader trascinamento/Alto/Basso intorno controlli in TableViewCells

self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension; self.tableView.estimatedSectionHeaderHeight = 25;

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    //----------------------------------------------------------------------------------- 
    //DYNAMIC TEXT and TABLE ROW HEIGHT 
    //----------------------------------------------------------------------------------- 
    self.tableView.estimatedRowHeight = 80.0f; 
    //also done in heightForRowAtIndexPath 
    self.tableView.rowHeight = UITableViewAutomaticDimension; 

    self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension; 
    self.tableView.estimatedSectionHeaderHeight = 80.0f; 

} 
//comment out 
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 

Vedi anche Is it possible to obtain a dynamic table view section header height using Auto Layout?

+0

Come non imposti le costanti larghezza/altezza nello storyboard? Anche se provi a impostare l'altezza dell'intestazione su 0, ti limiti alla correzione 1 – SwiftMatt

+0

Posso impostare le larghezze ma non ho mai impostato altezze FISSE (ad esempio altezza = 44) in modo che l'etichetta cresca verso il basso. Se ho bisogno di un'altezza minima, l'ho impostato in modo che sia SUPERIORE O UGUALE all'altezza> = 44 utilizzando il menu a discesa in IB. Funziona ma IB può lamentarsi di avvertimenti. Penso che il contenuto che abbraccia abbia bisogno di essere ottimizzato per ottenerlo approvato al 100% da IB –

0

Si dovrà creare un headerView personalizzato. Questo è quello che ho fatto: Swift 3, dovresti personalizzarlo per il tuo uso:

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
//Need to create a label with the text we want in order to figure out height 
    let label: UILabel = createHeaderLabel(section) 
    let size = label.sizeThatFits(CGSize(width: view.width, height: CGFloat.greatestFiniteMagnitude)) 
    let padding: CGFloat = 20.0 
    return size.height + padding 
} 

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let headerView = UITableViewHeaderFooterView() 
    let label = createHeaderLabel(section) 
    label.autoresizingMask = [.flexibleHeight] 
    headerView.addSubview(label) 
    return headerView 
} 

func createHeaderLabel(_ section: Int)->UILabel { 
    let widthPadding: CGFloat = 20.0 
    let label: UILabel = UILabel(frame: CGRect(x: widthPadding, y: 0, width: self.view.width - widthPadding, height: 0)) 
    label.text = sectionTextArray[section]// Your text here 
    label.numberOfLines = 0; 
    label.textAlignment = NSTextAlignment.left 
    label.lineBreakMode = NSLineBreakMode.byWordWrapping 
    label.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle.subheadline) //use your own font here - this font is for accessibility 
    return label 
}  
Problemi correlati