2016-12-01 61 views
0

我使用此代碼爲每個單元格靜態設置tableView單元格的高度。用swift動態設置TableViewCell的大小

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 
      } 
} 

我不想爲WebViewCell設置靜態值1000,而是使用WebView請求網站的高度。問題是我在設置DetailViewController類的高度後,在另一個名爲WebViewCell的類中請求網站。那可能嗎 ?

回答

1

viewDidLoad使用

tblView.rowHeight = UITableViewAutomaticDimension tblView.estimatedRowHeight = 200

和除去heightForRowAtIndexPath數據源的方法。另外不要忘記在UITableViewCell中使用AutoLayout

欲瞭解更多信息檢查http://www.appcoda.com/self-sizing-cells/

+1

必須使用Autolayout這個 –

+0

但一個應該實現動態高度由您的答案 –

+0

我無法刪除heightForRowAtIndexPath,我需要它爲其他單元格,我想設置動態高度僅用於WebViewCell – user567

0

你可以做這個

這樣做,也是不要忘記使用NSLayoutConstraints導致該不會沒有它

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 
} 

注意工作:對不起如果我的答案格式sucka。我現在使用我的手機

+0

爲什麼使用'heightForRowAtIndexPath'委託方法兩次? – Poles

+0

@Poles其他代表是'estimatedHeightForRowAtIndexPath'而不是'heightForRowAtIndexPath' –

+0

@ user567:你可以試試這個。 – Poles

相關問題