在UITableView中alignment多个运行时生成的UILabel

我有一个UITableView需要通过列举类似的东西来支持内容

在这里输入图像说明

但棘手的部分是,“标签”的数量将随着每个单元而变化,有些可能只有4个,但也可能达到12个。此外,“值”可以是单个单词或短语至多两行上面的图片)。 所以我决定使用UIStackView来帮助我打包和调整用于显示这个的UIStackView 。 我目前卡在一个问题,当“标签”的长度不一样的时候,如: 在这里输入图像说明

即使“标签”的长度不同,我也需要将“值”的前端像第一个图像一样alignment。 有没有UIStackView允许的行为? 还是有另一种方法可以让我获得我需要的结果?

注意:每个“标签”和“价值”是在一个UIStackView ,我做到了alignment它们。

我也尝试使用string格式,但多个行的“值”将包装在标签下,而不是自己包装,就像我在图像中做的那样。

我尝试将所有的“标签”放在一个UIStackView ,将所有的“值”放在另一个UIStackView ,一旦“值”超过一行,我就无法让它们像图像一样alignment。

或者,如果这可能是我犯的一个错误,那么我就是这样创build了UIStackViews

  var higherCount = 0 if labels.count<values.count { higherCount = values.count } else { higherCount = labels.count } mainStackView = UIStackView(frame: CGRect(x: 0, y: 0, width: frame.width, height: frame.height)) for i in 0..<higherCount { var height:CGFloat = 20 if (values[i] as NSString).size(attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: UIFont.systemFontSize)]).width > frame.width { height = 42 } let stackViewToAdd = UIStackView(frame: CGRect(x: 0, y: 0, width: frame.width, height: height)) let label = UILabel(frame: CGRect(x: 0, y: 0, width: frame.width, height: height)) if labels.count<higherCount { label.text = "" } else { label.text = labels[i] } label.setContentCompressionResistancePriority(1000, for: .horizontal) label.setContentHuggingPriority(999, for: .horizontal) stackViewToAdd.addArrangedSubview(label) let value = UILabel(frame: CGRect(x: 0, y: 0, width: frame.width, height: height)) if values.count<higherCount { value.text = "" } else { value.text = values[i] } value.lineBreakMode = .byWordWrapping value.numberOfLines = 2 stackViewToAdd.addArrangedSubview(value) mainStackView?.addArrangedSubview(stackViewToAdd) } mainStackView?.alignment = .fill mainStackView?.axis = .vertical mainStackView?.distribution = .fill 

我以编程方式创build了单元格,然后可以通过编程方式调整单元格的大小取决于UILabel内容的大小。

在我的情况下, Label字体是UIFont.systemFont(ofSize: 15) ,最小TableViewCell高度是50, arrLabel1arrLabel2将是标签的内容。

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return arrLable1.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell(style: .default, reuseIdentifier: "\(indexPath.row)") let lable1 = UILabel(frame: CGRect(x: 10, y: 10, width: view.frame.size.width - 20, height: 0)) lable1.numberOfLines = 0 lable1.font = UIFont.systemFont(ofSize: 15) lable1.text = arrLable1[indexPath.row] lable1.sizeToFit() cell.addSubview(lable1) let lable2 = UILabel(frame: CGRect(x: 10, y: lable1.frame.origin.y + lable1.frame.size.height + 10 , width: view.frame.size.width - 20, height: 0)) lable2.numberOfLines = 0 lable2.font = UIFont.systemFont(ofSize: 15) lable2.text = arrLable2[indexPath.row] lable2.sizeToFit() cell.addSubview(lable2) return cell } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { let boundingRect1 = arrLable1[indexPath.row].boundingRect(with: CGSize(width: view.frame.size.width - 40 , height: CGFloat(MAXFLOAT)), options: NSStringDrawingOptions.usesLineFragmentOrigin,attributes:[ NSFontAttributeName : UIFont.systemFont(ofSize: 15)] ,context: nil) let boundingRect2 = arrLable2[indexPath.row].boundingRect(with: CGSize(width: view.frame.size.width - 40 , height: CGFloat(MAXFLOAT)), options: NSStringDrawingOptions.usesLineFragmentOrigin,attributes:[ NSFontAttributeName : UIFont.systemFont(ofSize: 15)] ,context: nil) guard boundingRect1.height + boundingRect2.height + 30 > 50 else { return 50 } return boundingRect1.height + boundingRect2.height + 30 } 

Label numberOfLines设置为0