2017-07-03 98 views
0

我有一个表视图作为popUpTableView,因为我需要设置表视图高度动态为每行的行数。 我加入中的cellForRowAtIndexPath动态设置tableview高度并将其限制为swift 3?

popupTableView.height = popupTableView.contentSize.height 

它工作正常,但问题是在没有细胞的不断增加,则实现代码如下高度比其origingal高度的增加

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 
} 

如何为只有可见单元格和放置该代码的位置获取tableview高度?

在此先感谢。

+0

请参考以下链接:HTTPS://stackoverflow.com/questions/40034506/how-to-resize-table-cell-based-on-textview/40035858#40035858 – Jay

+0

谢谢,但我试图限制表视图高度只有可见行 –

+0

@BhagyashreeMyanamwar你有答案吗? –

回答

1

使用此在您的popuptableview的viewdidLoad()

popupTableView.estimatedRowHeight = 120

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

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

为了得到动力高度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 
      } 
     } 

在这种情况下,不要忘记使用自动版式,欲了解更多信息See Here

+1

这会给出第一行和第二行的具体高度 –

+0

是的,我提到'在我的情况',因为@Bhagyashree没有给出他的完整源代码,我希望他能处理其余的。 @ Samarth – Roy

+0

btw。 。谢谢@Samarth,我编辑了我的代码。 – Roy

0

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

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

    if changeHeight < maxHeight { 
    popupTableView.height = changeHeight 
    } 
} 
0

采取一个变量,它caluculate的tableview

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

电话委托方法willDisplay的bottomconstraint值的tableview的

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 
     } 
    }