I have just started with IOS development and i am facing issue with UiTableView
.I want to create an Expandable cell.So after researching i tried but the code works in some phones and not some phones.So i wanted some help.
At beginning i wanted to show only first 3 labels and then on click i want to show all the labels
Code
//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
}