我正在开发一个项目,我在UITableViewCell中有一个UILabel,根据适合其UILabel(UITableViewAutomaticDimension)的内容排列其单元格大小.Hence,这个UITableView具有可扩展的可折叠单元格,因此我设置了如果我的单元格已折叠,则行数= 5。我的要求是如果第五行包含一个新行,则设置行数= 4。由于现在的这种复杂情况,如果它有一个新行,我在第五行得到一个空白区域。我的代码如下。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell1", for: indexPath)
let description = cell.viewWithTag(tableViewCellTags.namelabel) as! UILabel
description.text = jsondata["text"] as! String
description.sizeToFit()
let seeMoreButton = cell.viewWithTag(tableViewCellTags.button) as! UIButton
seeMoreButton.addTarget(self, action: #selector(self.seeMoreClicked(button:)), for: UIControlEvents.touchDown)
cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()
description.numberOfLines = isHealerDetailCollapsed ? 5 : 0
loadTheInitialLabelText(descriptionContentLbl: description, descriptionSeeMoreBtn: seeMoreButton)
return cell
}else {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
return cell
}
}
func loadTheInitialLabelText(descriptionContentLbl:UILabel,descriptionSeeMoreBtn:UIButton) {
let DescriptionTextheight = descriptionContentLbl.text?.height(withConstrainedWidth: descriptionContentLbl.frame.width, font: descriptionContentLbl.font)
//let hac = "nednkewnlkednkewnklew".components(separatedBy: "")
// descriptionContentLbl.text
if descriptionContentLbl.intrinsicContentSize.height < DescriptionTextheight! {
descriptionSeeMoreBtn.isHidden = false
}else{
if(isSeemoreVisible){
descriptionSeeMoreBtn.isHidden = true
}
}
}
}
extension String {
func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat {
let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
return boundingBox.height
}
}