Ios 计算视角高度

Ios 计算视角高度,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个UICollectionView子类,我正试图将它嵌入UITableView行中。它已经起作用了,但我无法计算出正确的高度。我是这样做的: func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { if indexPath.row==2{ if(self.contentViewCell == nil){ self.co

我有一个
UICollectionView
子类,我正试图将它嵌入
UITableView
行中。它已经起作用了,但我无法计算出正确的高度。我是这样做的:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
  if indexPath.row==2{
    if(self.contentViewCell == nil){
      self.contentViewCell = tableView.dequeueReusableCellWithIdentifier(Storyboard.containerCellIdentifier)
      let matchDetailView = getMatchDetailView()
      matchDetailView.frame = self.contentViewCell!.contentView.frame
      self.contentViewCell!.contentView.addSubview(matchDetailView)
    }

    self.contentViewCell?.layoutIfNeeded()
    let size = self.contentViewCell?.contentView.systemLayoutSizeFittingSize(UILayoutFittingExpandedSize)
    return size!.height + 1
  }
  return UITableViewAutomaticDimension
}
不幸的是,高度总是错误的。谁能给我指一下正确的方向吗

它也需要在iOS 7上工作

编辑:


getMatchDetailView()
从情节提要实例化ViewController并返回其视图。

是否使用了
UITableViewAutomaticDimension
UITableViewAutomaticDimension
在iOS 7上不可用在
ViewDidDisplay中重新加载collectionview
@Nishant您的意思是我应该在
viewdide是否显示我的tableviewcontroller的功能?这有什么意义?我需要在
heightforrowatinexpath
中返回正确的高度-重新加载collectionView不会有任何错误effect@gasparuff你在使用自动布局吗?如果是,
self.contentViewCell!。contentView.frame
可能无法返回正确的值,直到
viewDidLoad