【问题标题】:UITableViewAutomaticDimension is not workingUITableViewAutomaticDimension 不起作用
【发布时间】:2019-04-19 14:03:38
【问题描述】:

我已将 tableview 的估计高度和高度设置为 UIAutomaticDimension,但标签高度增加了。

我尝试更改label.preferredMaxLayoutWidth,但仍然无法正常工作。

我已将 tableview 的估计高度和高度设置为 UIAutomaticDimension,但标签高度增加了。

我尝试更改label.preferredMaxLayoutWidth,但仍然无法正常工作。

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

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if let buildingArr = buildingViolationArray {
        return buildingArr.count
    }
    return 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: Language.sharedInstance.isEnglish ? "CELL" : "CELL_AR", for: indexPath) as! BuildingViolationHeaderTableViewCell
    if let buildingViolationsDict = buildingViolationArray?[indexPath.row] {

        cell.followUpNoLbl.text = buildingViolationsDict["followupNo"] as? String
        cell.violationTypeLbl.text = buildingViolationsDict[Language.sharedInstance.isEnglish ? "violationType" : "violationTypeArb"] as? String
        cell.bvBtn.addTarget(self, action: #selector(BuildinVioClicked), for: .touchUpInside)
        if buildingViolationsDict[kIsSelectedKey] as? Bool == true {
            cell.isCellSelected = true
            let buildingVioView = getZoneRegView(buildingViolationsDict)
            buildingVioView.tag = 1
            for removeSubViews in cell.bvStackView.subviews {
                removeSubViews.removeFromSuperview()
                cell.bvStackView.removeArrangedSubview(removeSubViews)
            }

            cell.bvStackView.addArrangedSubview(buildingVioView)
            cell.expandImage.image = UIImage(named: "minus-256")
        } else {
            cell.isCellSelected = false
            for removeSubViews in cell.bvStackView.subviews {
                removeSubViews.removeFromSuperview()
                cell.bvStackView.removeArrangedSubview(removeSubViews)
            }
            cell.expandImage.image = UIImage(named: "plus-256")
        }

        cell.violationTypeLbl.preferredMaxLayoutWidth = cell.violationTypeLbl.frame.size.width
    }

    cell.selectionStyle = .none

    return cell
}

func BuildinVioClicked(sender: UIButton){
    let location = sender.convert(CGPoint.zero, to: bvTableView)
    let indexPath = bvTableView.indexPathForRow(at: location)!
    if var buildingViolationsDict = buildingViolationArray?[indexPath.row] {
        if let isSelect = buildingViolationsDict[kIsSelectedKey] as? Bool, isSelect {
            (buildingViolationArray[indexPath.row])[kIsSelectedKey] = false
        } else {
            (buildingViolationArray[indexPath.row])[kIsSelectedKey] = true
        }
        bvTableView.reloadData()
    }
}

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

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

【问题讨论】:

  • 请提供有关问题的足够信息。提供您编写的代码。
  • 到目前为止您尝试过什么? , 在您尝试过某事但没有成功后,我们会在这里为您提供帮助。尝试详细描述您的问题。
  • 将估计高度设置为估计值而不是UITableViewAutomaticDimension 还要确保您放置了适当的约束,以便自动尺寸正常工作
  • 我建议尝试一个更简单的单元格设计,里面只有一个标签,看看它是否有效。之后,您可以在其上添加您的设计并隔离问题。

标签: ios iphone swift ipad ios7


【解决方案1】:

除了将estimatedRowHeight 设置为UITableViewAutomaticDimension(在Swift 4 中,UITableViewAutomaticDimension 已重命名为UITableView.automaticDimension)。您应该正确设置单元格约束。

class YourCell: UITableViewCell {
  lazy var label: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.font = UIFont.systemFont(ofSize: 12)
    label.numberOfLines = 0 // This property will allow the label according to his content
    return label
  }()

  func addLabelIntoView() {
    contentView.addSubview(label)
    let margin = contentView.layoutMarginsGuide
    NSLayoutConstraint.activate([
      label.leadingAnchor.constraint(equalTo: margin.leadingAnchor),
      label.trailingAnchor.constraint(equalTo: margin.trailingAnchor),
      label.topAnchor.constraint(equalTo: margin.topAnchor),
      label.bottomAnchor.constraint(equalTo: margin.bottomAnchor) // Pins bottom of label into the bottom of the view
      ])
  }
}

【讨论】:

    猜你喜欢
    • 2015-07-29
    • 2016-04-07
    • 1970-01-01
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    相关资源
    最近更新 更多