我刚刚开始使用IOS开发,我遇到问题UiTableView
。我想创建一个可扩展的单元格。所以经过研究我试过但是代码适用于某些手机而不是某些手机。所以我想要一些帮助。
一开始我想只显示前3个标签,然后点击我想显示所有标签
代码
//to calculate expanded height
func expandedHeight() -> CGFloat
{
let data = arrayDrugs[selectedIndex!.row]
let cell = tableView.dequeueReusableCellWithIdentifier("expandedCell") as! UDCell
let height = returnLableHeight(cell.lblProducts, fontSize: 16, text: data.packedProd) +
returnLableHeight(cell.lblSecondData, fontSize: 15, text: data.dose) +
returnLableHeight(cell.lblThirdData, fontSize: 15, text: data.secondData) +
returnLableHeight(cell.lblFourthData, fontSize: 15, text: data.thirdData) +
returnLableHeight(cell.lblFifthData, fontSize: 15, text: data.fourthData) +
returnLableHeight(cell.lblSixthData, fontSize: 15, text: data.fifthData) +
returnLableHeight(cell.lblSeventhData, fontSize: 15, text: data.sixthData)
print(height);
print((cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingExpandedSize).height)+1)
cell.sizeToFit()
cell.setNeedsLayout()
cell.layoutIfNeeded()
return (cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingExpandedSize).height);
}
//to calculate collapsed height
func collapseHeight(indexPath:NSIndexPath) -> CGFloat{
let data = arrayDrugs[indexPath.row]
let cell = tableView.dequeueReusableCellWithIdentifier("expandedCell") as! UDCell
let height = returnLableHeight(cell.lblProducts, fontSize: 16, text: data.packedProd) +
returnLableHeight(cell.lblSecondData, fontSize: 15, text: data.dose) +
returnLableHeight(cell.lblThirdData, fontSize: 15, text: data.secondData)
//print(height);
// print((cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingExpandedSize).height)+1)
cell.sizeToFit()
cell.setNeedsLayout()
cell.layoutIfNeeded()
return (height+120);
}
//calculate label height
func returnLableHeight(label:UILabel,fontSize:CGFloat,text:NSAttributedString) -> CGFloat{
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.ByWordWrapping
label.font = UIFont.systemFontOfSize(fontSize)
label.preferredMaxLayoutWidth = self.view.frame.width-40;
label.attributedText = text
// let rect:CGRect = text.boundingRectWithSize(CGSizeMake(self.view.frame.width-25,CGFloat.max), options: NSStringDrawingOptions.UsesLineFragmentOrigin, context:nil )//hear u will get nearer height not the exact value
// let requredSize:CGRect = rect
label.sizeToFit()
//print(requredSize.height)
return label.frame.height
}