如何将 uilabel 高度设置为 0。或将 uibleviewcell 高度设置为 0?

how to set uilabel height to 0. or uitbleviewcell height to 0?

我有一个这样的页面

现在,如果我得到任何值 nil 我会隐藏该标签

问题:

但是当我隐藏该标签时,它显示为空白 space

这是我的代码::

 let dic = finalcarexpense.objectAtIndex(indexPath.row) as! NSDictionary
    if lastcruisebool == true
    {
        cell.lblcruisecontroll.text = (dic["cruise_control"] as? String)!
    }
    else
    {

        cell.lblcruisecontroll.hidden = true
        cell.placecruisecontrol.hidden = true
    }



    if lastremotebool == true
    {
        cell.lblremotecentrallocking.text = (dic["remote_central_locking"] as? String)!
    }
    else
    {
        cell.lblremotecentrallocking.hidden = true
        cell.placeremotecentrallocking.hidden = true
    }


    if lastacbool == true
    {
        cell.lblairconditioning.text = (dic["air_conditioning"] as? String)!
    }
    else
    {
        cell.lblairconditioning.hidden = true
        cell.placeac.hidden = true
    }


    if lastbluetoothbool == true
    {
        cell.lblbluetooth.text = (dic["bluetooth"] as? String)!
    }
    else
    {
        cell.lblbluetooth.hidden = true
        cell.placebluetooth.hidden = true
    }


    if lastradiocdbool == true
    {
        cell.lblradiocd.text = (dic["radio_cd"] as? String)!
    }
    else
    {

        cell.lblradiocd.hidden = true
        cell.placeradiocd.hidden = true
    }


    if lastpowerbool == true
    {
        cell.lblpowersteering.text = (dic["power_steering"] as? String)!
    }
    else
    {
        cell.lblpowersteering.hidden = true
    }

那么有什么解决方案吗,比如我应该设置 UILabel hieght 0 吗?或者我应该使用 UITableViewCell??

最好的解决方案是更改您的架构与 table 一起工作。事实上,您应该更改单元格的数量。您需要在模型中存储数据,它甚至可能是一个数组,您的 table 应该是数据的显示,如果不需要单元格,您可以将其从模型中删除并重新加载 table .为此,您需要对单元格使用 Dynamic Prototypes,并使用此方法来处理它:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return yourDataModel.count
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 100
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("YourCell", forIndexPath: indexPath) as! YourCellController
    return cell
}

例如阅读本教程 http://www.techotopia.com/index.php/Using_Storyboards_and_Swift_to_Build_Dynamic_TableViews_with_Prototype_Table_View_Cells 以更好地理解如何操作

一个解决方案是(使用 NSLayoutConstraint 的常量 属性 )所有标签高度的 IBOutlet 连接并将它们的高度设置为零。这将消除你的间距。

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentViewHeightConstraint;

例如。 lblairconditioningCostraintHeight.constant=0

检查地址字段的高度限制。我为此制作了 IBOutlet,如果地址很大,它会自动将所有标签向下推,反之亦然。

如果 label.hope 中没有文本,则在 cellForRowAtIndexPath 方法中将标签高度设置为零,这将对您有所帮助。

你不应该用一个单元格来容纳所有 rows/items 来显示。如果您喜欢这样,那么很难管理标签之间的空白 space。

您必须调整标签之间的垂直间距约束,即使您将标签的值设置为 nil。这是一个复杂的问题。

最好采用以下方法。

  1. 清除 UITableView 分隔符颜色

  2. 每个 row/cell 应该 重复使用 单个原型单元格。 1 个单元格。

  3. 使用UITableViewDelegate的方法

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        If(indexPath.row should be should not be shown){
             return 0;//for hiding the row
        }
        return Default Height Of row;
    
    }
    

    希望以后有人用得上。

对所有标签添加高度约束,拖动Outlet进行约束如下:

然后,如果服务器的响应为 nil,则将高度限制设置为 0,如下所示:

 nsLcHeightForLabel.constant = 0

可以给uilabel高度约束,可以在运行时设置为0。你也可以将它的 frame 设置为 0 这样它就看不见了 谢谢