2016-12-01 63 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

相关问题