作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 TableView 作为 popUpTableView,因为我需要根据行数动态设置 TableView 高度。我正在添加 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
}
如何仅获取可见单元格的 tableview 高度以及放置该代码的位置??
提前致谢。
最佳答案
要获取 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
关于ios - 动态设置桌面 View 高度并限制在一定范围内 swift 3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44883797/
我正在尝试开发右边框/Angular 具有特定 Angular (30°) 的表格。我见过一些类似的解决方案,但它们都无法在一定程度上发挥作用。如果我想从 30° 改变到 20°,我不想花太多力气。
我是一名优秀的程序员,十分优秀!