0

我有一个UITableView(表1)。在UITableView Cell中我有另一个UITableView(表2)。TableView单元格动态高度

Table 2行的高度是UITableViewAutomaticDimension

我的要求是 - 我想这Table 2不应滚动,并采取充分的高度,每个里面it.So的行数,按本Table 2的高度,我需要设置Table 1的行高。

我使用表2的contentSize.height,但它不工作。我搜查了很多,但什么也没找到。

代码:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 
    { 
     let cell = tableView_First.dequeueReusableCell(withIdentifier: "SecondTableViewCell") as! SecondTableViewCell 

     return cell.second_TableView.contentSize.height 
    } 

更新:

我已经调试的代码,发现表2的电池的估计高特效表1的单元格的高度。即,如果表2的单元的估计高度是100并且这些单元是5个单元。然后这cell.second_TableView.contentSize.height给我500.哪个是错误的。因为细胞有不同的高度10,20,30,40,10。

任何建议,将不胜感激。

谢谢!

回答

0

可能你正在因为以下两个原因

  1. 当你得到cell.second_TableView.contentSize.height那个时候你cell.second_TableView可能不会完全加载因此你得到错误高度的错误的结果。

  2. 您正在清除新的单元而不是现有的已加载单元。

请尝试以下代码:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 
    { 

//Get existing cell rather deque new one 
     let cell = tableView_First.cellForRowAtIndexPath(indexPath) as! SecondTableViewCell 

//This will force table view to load completely 
    cell.second_TableView.layoutIfNeeded() 

     return cell.second_TableView.contentSize.height 
    } 

希望这将解决您的问题。

+0

感谢您的回答。但是这段代码给了我“Bad _Access”。我检查了错误https://stackoverflow.com/questions/35931478/why-tableview-cellforrowatindexpathindexpath-give-me-a-bad-access-error。你能建议现在做什么? – Amanpreet

0

写入这两条线在viewDidLoad中,

tableView.estimatedRowHeight = 241

tableView.rowHeight = UITableViewAutomaticDimension

FUNC的tableView(_的tableView:UITableView的,heightForRowAt indexPath:IndexPath) - > CGFloat的{ return UITableViewAutomaticDimension }

相关问题