【问题标题】:set tableview height dynamically and restrict to some extend swift 3?动态设置tableview高度并限制到一些扩展swift 3?
【发布时间】:2017-12-06 14:49:33
【问题描述】:

我有一个作为 popUpTableView 的表格视图,因为我需要根据行数动态设置表格视图高度。 我正在添加 cellForRowAtIndexpath

  popupTableView.height = popupTableView.contentSize.height

它工作正常,但问题是当没有单元格增加时,tableview 的高度比原来的高度增加了

extension DiagnosisViewController :UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if tableView == diagnosisTableView {
            return self.diagnosisModel.count
        } else if tableView == popupTableView {
            return self.popupDiagnosisModel.count
        }
        return 0
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        var cell:DiagnosisTableViewCell?

        if tableView == self.diagnosisTableView {

            cell = self.diagnosisTableView.dequeueReusableCell(withIdentifier: "DiagnosisCell") as? DiagnosisTableViewCell



            if diagnosisModel.count > 0 {

                let model = diagnosisModel[indexPath.row]
                cell?.indexLabel.text =  String(indexPath.row + 1)
                cell?.diagnosisNameLabel.text = "  \(model.diagnosisDescription!)"
                cell?.deleteButton.tag = indexPath.row
                cell?.deleteButton.addTarget(self, action:#selector(deleteDiagnosis(button:)), for: .touchUpInside)
                diagnosisNotFoundLabel.isHidden = true

            } 

        } else if tableView == self.popupTableView {


            cell = self.diagnosisTableView.dequeueReusableCell(withIdentifier: "DiagnosisCell") as? DiagnosisTableViewCell

            cell?.deleteButton.isHidden = true

            if (indexPath.row % 2 == 0) {
                cell?.baseView.backgroundColor = UIColor.white
            }
            else {
                cell?.baseView.backgroundColor = UIColor.init(colorLiteralRed: 241/255, green: 241/255, blue: 241/255, alpha: 1.0)
            }

            cell?.indexLabel.text =  String(indexPath.row + 1)
            cell?.indexView.layer.cornerRadius = (cell?.indexView.frame.size.width)!/2
            cell?.indexView.layer.borderWidth = 0.5
            cell?.indexView.layer.borderColor = UIColor.lightGray.cgColor

            cell?.baseView.layer.borderWidth = 0.5
            cell?.baseView.layer.cornerRadius = 5.0
            cell?.baseView.layer.borderColor = UIColor.lightGray.cgColor;

            if diagnosisModel.count > 0 {

                let model = popupDiagnosisModel[indexPath.row]
                cell?.diagnosisNameLabel.text = "  \(model.diagnosisDescription!)"
            }
            self.popupTableView.height = self.popupTableView.contentSize.height 

        }
        return cell!
    }

}


let maxHeight = popupTableView.frame.size.height
let changeHeight = popupTableView.contentSize.height

if changeHeight < maxHeight {
popupTableView.height = changeHeight
}

如何仅获取可见单元格的表格视图高度以及该代码的放置位置??

提前致谢。

【问题讨论】:

  • 谢谢,但我正在尝试将表格视图高度限制为仅可见行
  • @BhagyashreeMyanamwar 你得到答案了吗?
  • @BhagyashreeMyanamwar 因此您正在使用两个表格视图,并且您想要更改正在显示的那个的高度,无论是popupTableView 还是diagnosisTableView
  • @BhagyashreeMyanamwar 您对两个表格视图单元格使用相同的标识符吗? DiagnosisCell

标签: ios uitableview uiview swift3


【解决方案1】:

取一个变量来计算tableview的bottomconstraint值

 var bottomConstraintValue : CGFloat = 0.0
    override func viewDidLoad() {
        super.viewDidLoad()
   bottomConstraintValue = self.popupContainerView.height - self.popupTableView.frame.origin.y - self.popupTableView.frame.size.height
     }

调用tableview的委托方法willDisplay:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

if tableView == popupTableView {
        if indexPath.row == (popupTableView.indexPathsForVisibleRows?.last)?.row {
            adjustTableViewHieght(popupTableView: self.popupTableView,popupContainerView: self.popupContainerView , bottomConstraintValue: self.bottomConstraintValue)
        }
    }
}

    func adjustTableViewHieght(popupTableView: UITableView,popupContainerView: UIView , bottomConstraintValue:CGFloat){
        let contentSizeHeight = popupTableView.contentSize.height;
        let tableViewHieghtWithBottomSpace = popupContainerView.height - popupTableView.frame.origin.y
        let actualTableViewHieght = (tableViewHieghtWithBottomSpace - bottomConstraintValue)
        if (contentSizeHeight > actualTableViewHieght) {
            popupTableView.height = actualTableViewHieght
        } else {
            popupTableView.height = contentSizeHeight
        }
    }

【讨论】:

    【解决方案2】:

    尝试在viewWillLayoutSubviews方法中添加调整高度的代码。试试这个

    override func viewWillLayoutSubviews() {
      super.viewWillLayoutSubviews()
      let maxHeight = popupTableView.frame.size.height
      let changeHeight = popupTableView.contentSize.height
    
      if changeHeight < maxHeight {
      popupTableView.height = changeHeight
      }
    }
    

    【讨论】:

      【解决方案3】:

      要获得UITableViewCell动态高度,请返回UITableViewAutomaticDimension 作为您的行高。

      示例:(在我的情况下)

          func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 
              {
                  if indexPath.row == 0
                  {
                      return 200
                  }
                  else if indexPath.row == 1
                  {
                      return 10
                  }
                  else
                  {
                      return UITableViewAutomaticDimension
                  }
              }
      

      在这种情况下不要忘记使用 AutoLayout ,更多信息See Here

      【讨论】:

      • 这将为第一行和第二行提供特定的高度
      • 是的,我提到了“在我的情况下”,因为@Bhagyashree 没有给出他的完整源代码,我希望他能处理剩下的。 @萨玛斯
      • 让它成为有用的评论@Roy :)
      • 我回滚了,因为我让你的评论有用@Samarth
      • @Samarth,你能再检查一下这个问题吗?
      【解决方案4】:

      在弹出表视图的viewdidLoad() 中使用它:

      popupTableView.estimatedRowHeight = 120

      并创建一个设置自动尺寸高度的函数:

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

      【讨论】:

        猜你喜欢
        • 2021-09-18
        • 1970-01-01
        • 2017-11-10
        • 1970-01-01
        • 2017-04-18
        • 1970-01-01
        • 2015-04-25
        • 2015-10-18
        • 2019-04-07
        相关资源
        最近更新 更多