我有一個表視圖作爲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高度?
在此先感謝。
請參考以下鏈接:HTTPS://stackoverflow.com/questions/40034506/how-to-resize-table-cell-based-on-textview/40035858#40035858 – Jay
謝謝,但我試圖限制表視圖高度只有可見行 –
@BhagyashreeMyanamwar你有答案嗎? –