2016-12-01 8 views
0

Ich setze Höhen von TableView Zellen statisch für jede Zelle mit diesem Code.Dynamisch die Größe von TableViewCell mit swift festlegen

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 

if let cell = modelCollection[collection.identifier] { 
      if let _ = cell as? TiteCell{ 
       return 200 
      }else if let _ = cell as? ParagraphCell{ 
       return 500 
      } else if let _ = cell as? WebViewCell{ 
       return 1000 
      } 
} 

Ich will nicht mehr den statischen Wert 1000 für das WebViewCell setzen, sondern die Höhe der Website mit dem WebView angefordert. Das Problem besteht darin, dass ich die Website in einer anderen Klasse namens WebViewCell anfordere, nachdem ich die Höhe der DetailViewController-Klasse festgelegt habe. Ist das möglich ?

Antwort

1

im viewDidLoad Verwendung

tblView.rowHeight = UITableViewAutomaticDimension tblView.estimatedRowHeight = 200

und heightForRowAtIndexPath Datenquelle Methode entfernen. Vergessen Sie auch nicht AutoLayout in UITableViewCell zu verwenden.

Für weitere Informationen überprüfen http://www.appcoda.com/self-sizing-cells/

+1

müssen Autolayout für diese –

+0

verwenden, aber sollte eine dynamische Höhe durch Ihre Antwort erreichen –

+0

Ich kann heightForRowAtIndexPath nicht entfernen, ich brauche es für die anderen Zellen, ich möchte die dynamische Höhe einstellen nur für WebViewCell – user567

0

Sie es auch nicht vergessen, indem Sie diese

tun konnte NSLayoutConstraints dies ohne es

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 

    if let cell = modelCollection[collection.identifier] { 
      if let _ = cell as? TiteCell{ 
       return 200 
      }else if let _ = cell as? ParagraphCell{ 
       return 500 
      } else if let _ = cell as? WebViewCell{ 
       return UITableViewAutomaticDimension 
      } 
    } 


func tableView(tableView: UITableView, EstimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    return 1000 
} 

Hinweis nicht funktionieren verursachen zu verwenden: sorry wenn meine antwort formatierung sucka. Ich benutze mein Telefon im Moment

+0

Warum verwenden 'heightForRowAtIndexPath' Delegat Methoden zweimal? – Poles

+0

@Poles Der andere Delegat ist 'geschätztHeightForRowAtIndexPath' nicht 'heightForRowAtIndexPath' –

+0

@ user567: Sie können dies versuchen. – Poles

Verwandte Themen