【问题标题】:UITableview Auto Adjust Row Issue Swift IOS?UITableview 自动调整行问题 Swift IOS?
【发布时间】:2018-06-07 21:50:26
【问题描述】:

我面临一个动态调整UITableView 行高的问题。我知道通过使用这种方法我们可以实现

self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 44.0; 

但在我的情况下,我将 XIB 文件用于 UITableViewCell,并且我正在为单元格添加阴影。在这种情况下如何动态添加行高。同时,根据服务器时间,我正在显示和隐藏按钮。所以请任何人都可以建议我如何解决这个问题。这是我的行索引方法的单元格。

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identifier = "Custom"
        var cell: PApplyLeaveTableViewCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? PApplyLeaveTableViewCell
        if cell == nil {
            tableView.register(UINib(nibName: "PApplyLeaveTableViewCell", bundle: nil), forCellReuseIdentifier: identifier)
            cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? PApplyLeaveTableViewCell
        }
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        cell.contentView.backgroundColor = UIColor.clear
        var localDic :NSDictionary!
        localDic = totlLeavesArray.object(at: indexPath.row) as! NSDictionary
        cell.acknowled_lbl.text = localDic["acknowledgement"] as? String
        cell.date_lbl.text = localDic["totalDate"] as? String
        cell.reason_lbl.text = localDic["reason"] as? String
        let compareDate = localDic["compare"] as? String
        if(compareDate == "No")
        {
            cell.delet_Btn.isHidden = true
            cell.edit_Btn.isHidden = true
        }
        else
        {
            cell.delet_Btn.isHidden = false
            cell.edit_Btn.isHidden = false
        }
        cell.edit_Btn.tag = indexPath.row
        cell.edit_Btn.addTarget(self, action: #selector(PApplyLeaveViewController.EditViewAction(_:)), for:.touchUpInside)
        cell.delet_Btn.tag = indexPath.row

        cell.delet_Btn.addTarget(self, action: #selector(PApplyLeaveViewController.DeleteAction(_:)), for:.touchUpInside)
        let whiteRoundedView : UIView = UIView(frame: CGRect(x: 5, y: 8, width: self.view.frame.size.width - 15, height: 220))
        whiteRoundedView.layer.backgroundColor = CGColor(colorSpace: CGColorSpaceCreateDeviceRGB(), components: [1.0, 1.0, 1.0, 0.9])
        whiteRoundedView.layer.masksToBounds = false
        whiteRoundedView.layer.cornerRadius = 2.0
        whiteRoundedView.layer.shadowOffset = CGSize(width: -1, height: 1)
        whiteRoundedView.layer.shadowOpacity = 0.2
        cell.contentView.addSubview(whiteRoundedView)
        cell.contentView.sendSubview(toBack: whiteRoundedView)
        cell.contentView.backgroundColor = UIColor.clear
        return cell
    } 

【问题讨论】:

    标签: ios iphone swift uitableview row-height


    【解决方案1】:

    通过 AutoLayout 约束设置标签框架,然后构建并运行

    【讨论】:

      【解决方案2】:

      所以我假设这里的问题是单元格保持在其估计的 44 点高度。您遇到的问题是,尽管单元格的子视图可能会发生变化,但与单元格框架大小本身没有任何关系。例如,您将 whiteRoundedView 设置为特定大小,但这将如何影响单元格本身的大小。

      处理这种情况有两种基本方法:

      1) 不使用单元格的动态高度,而是使用此方法根据应用状态确定每个单元格的高度

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

      2) 切换到使用自动布局和设置约束将单元格大小与其内容相关联。

      还有一点最终会反过来咬你,那就是每次系统需要包含现有单元出列时的单元格时,你都会添加一个 whiteRoundedView。出队的单元格在创建时已经添加了 whiteRoundedView。您真的想将该代码移动到您创建单元格的位置,如下所示:

      if cell == nil {
          tableView.register(UINib(nibName: "PApplyLeaveTableViewCell", bundle: nil), forCellReuseIdentifier: identifier)
          cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? PApplyLeaveTableViewCell
      
          let whiteRoundedView : UIView = UIView(frame: CGRect(x: 5, y: 8, width: self.view.frame.size.width - 15, height: 220))
          whiteRoundedView.layer.backgroundColor = CGColor(colorSpace: CGColorSpaceCreateDeviceRGB(), components: [1.0, 1.0, 1.0, 0.9])
          whiteRoundedView.layer.masksToBounds = false
          whiteRoundedView.layer.cornerRadius = 2.0
          whiteRoundedView.layer.shadowOffset = CGSize(width: -1, height: 1)
          whiteRoundedView.layer.shadowOpacity = 0.2
          cell.contentView.addSubview(whiteRoundedView)
          cell.contentView.sendSubview(toBack: whiteRoundedView)
          cell.contentView.backgroundColor = UIColor.clear
      }
      

      所以它只添加一次。

      事实上,如果您使用的是 XIB,为什么不在其中设置阴影视图,然后调整参数。

      【讨论】:

      • 首先感谢您回复我。让我试试你的建议。
      • 例如,我在 uitableviewcell 背景视图中为标签和按钮拍摄一个 uiview。我将为该视图设置阴影,直到这里一切都很好。但在我的情况下,按钮有时会出现,有时会隐藏如何调整行高
      • 正如我在回答中所说,您可以使用方法 1 并直接在委托中指定行高,或者 2 使用自动布局并对所有内容设置约束以将视图绑定到单元格以允许它确定它的高度。
      • 在您的第一个选项中,如果我静态设置行,它将如何使用行数据动态调整行高
      • 为了让 UITableView 动态调整行高,您确实需要使用自动布局并设置适当的约束。现在,如果你不明白它是如何工作的,你需要查看文档并找到一些教程,因为这里需要解释一个很大的话题。我的第一个选择是放弃让 UITableView 和单元格自动调整高度并让委托指定它们。
      【解决方案3】:

      我通过设置约束值解决了我的问题。我将标签底部约束设置为“容器底部空间”。这是我处理按钮显示和隐藏的代码。但我无法相应地修复行高。但不知何故我正在解决设计问题。

      if compareDateString == currentDateString
              {
                  cell.delete_Btn.isHidden = true
                  cell.edit_Btn.isHidden = true
                  cell.bottomConstraint.constant = 30
              }
      
              else if compareDateString! < currentDateString
              {
                  cell.delete_Btn.isHidden = true
                  cell.edit_Btn.isHidden = true
                cell.bottomConstraint.constant = 30
              }
              else
              {
                  cell.delete_Btn.isHidden = false
                  cell.edit_Btn.isHidden = false
                  cell.bottomConstraint.constant = 45
      
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-22
        • 1970-01-01
        • 2014-12-01
        • 2012-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多