2017-07-16 71 views
0

我有一个UITableView。在我的tableview中,我想动态添加另一个视图到我的单元格中,并扩大单元格高度。我看到了一些例子,但是这些例子是UILabels。 UILabels可以根据文本高度更改高度。但我怎么手动添加另一个视图并展开这些单元格?如何动态扩展UITableViewCell高度?

请帮我 感谢

回答

0

如果使用手动计算单元格的高度,它很容易,你加入另一个视图后,你计算的高度,然后调用reloadData或reloadRowsAtIndexPaths,并在tableview中返回的高度: heightForRowAtIndexPath函数。 如果你使用自动计算单元格高度,你应该让系统知道如何计算,你应该清楚和完全地设置自动布局。你应该添加另一个视图的左,右,上,下约束,系统将自动可以调整高度。因此细胞将会扩大。

+0

谢谢,,为table.rowheight我应该设置默认大小还是将其设置为动态? – user1960169

+0

你可以像这样设置:self.tableView.estimatedRowHeight = 100; //像所有单元一样的值 self.tableView.rowHeight = UITableViewAutomaticDimension; – zacks

0

您可以通过覆盖设置单元格的高度func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat

0

实现这两个的UITableViewDelegate方法:

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

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { 
    // return estimatedHeight 
    } 

查看在动态细胞需要有顶部,底部,尾随,领导和高度的限制。设置视图高度约束常数将根据视图高度设置单元高度。

您可以查看高度约束属性在动态细胞:

@IBOutlet weak var customViewHeightConstraint: NSLayoutConstraint! 

约束恒定值,可以在cellForRowAt改变:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "CellId", for: indexPath) as! Cell 

    switch indexPath.row { 
    case 0: 
     cell.customViewHeightConstraint.constant = 100 
    case 1: 
     cell.customViewHeightConstraint.constant = 200 
    case 2: 
     cell.customViewHeightConstraint.constant = 300 
    case 3: 
     cell.customViewHeightConstraint.constant = 400 
    default: 
     cell.customViewHeightConstraint.constant = 100 
    } 

    return cell 
} 
+0

我这样做,如果(arraycontainsthisvalue)然后更改heigt = 100其他高度= 50。但有时它在错误的情况下给了我错误的高度,它仍然给我100为什么是这样? – user1960169