【问题标题】:how to set uilabel height to 0. or uitbleviewcell height to 0?如何将 uilabel 高度设置为 0。或 uitbleviewcell 高度设置为 0?
【发布时间】:2015-12-23 05:51:03
【问题描述】:

我有一个这样的页面

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

问题:

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

这是我的代码::

 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来做到这一点??

【问题讨论】:

  • 您应该过滤包含数据的数组,而不是将 Height 设置为 0
  • 但数组中没有数据@HamzaAnsari
  • 它基于用户选择@HamzaAnsari
  • 从tableview中删除依赖于数据的单元格。
  • 从 uitableview 中删除单元格的代码是什么? @iOSGuru

标签: ios swift uitableview uilabel ios9


【解决方案1】:

您可以给 uilabel 高度约束,并且可以在运行时设置为 0。您也可以将其设置为 0 使其不可见 谢谢

【讨论】:

    【解决方案2】:

    您不应该使用单个单元格来保存所有要显示的行/项目。如果你喜欢,那么管理标签之间的空白空间是非常困难的。

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

    最好采用以下方法。

    1. 清除 UITableView 分隔符颜色

    2. 每一行/单元格都应该重用单个原型单元格。 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;
      
      }
      

      我希望将来有人会使用它。

    【讨论】:

      【解决方案3】:

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

      那么如果你得到 nil 来自服务器的响应,将高度约束设置为 0,如下所示:

       nsLcHeightForLabel.constant = 0
      

      【讨论】:

        【解决方案4】:

        最好的解决方案是改变你的架构与表格一起工作。实际上,您应该更改单元格的数量。您需要将数据存储在模型中,它甚至可能是一个数组,并且您的表格应该是数据的显示,如果不需要单元格,您可以将其从模型中删除并重新加载表格。为此,您需要将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 以更好地了解如何操作

        【讨论】:

          【解决方案5】:

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

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

          例如。 lblairconditioningCostraintHeight.constant=0

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

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

          【讨论】:

            猜你喜欢
            • 2012-07-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-09-27
            • 1970-01-01
            • 2020-01-18
            • 2016-08-21
            • 1970-01-01
            相关资源
            最近更新 更多