I am having a header named CollapsibleTableViewHeader
and its height is dynamic based on the UILabels
and UIViews
contained inside it.
So, I have this function below -
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let header = self.tableView.dequeueReusableHeaderFooterViewWithIdentifier("header") as! CollapsibleTableViewHeader
switch indexPath.row {
case 0:
if sections[indexPath.section].collapsed == true {
return 0
}
else {
return header.iconsViewHeightConstraint.constant + header.shortDescHeightConstraint.constant + header.validityDateHeightConstraint.constant + header.expiryAlertHeightConstraint.constant
}
default:
return 0
}
}
In my else condition, I need to return the height of my header as the height of my row. So I added up all the elements inside my header and returning the value (CGFloat).
The height is returned as expected but the problem is the same height is applied to all the cells. For e.g. if the height value returned is 150.0, then the same 150.0 is being applied to all the cells out there irrespective of their individual heights.
How do I get the heights specific to each cell? indexPath
is one critical thing to use, but I am not sure how to use it here.
Apologies, if the question is dumb! Please help
PS: I tried automaticDimension
already but that doesn't help in anyway since I have these cells as collapse/expandable cells.
EDIT 1: Added viewForHeaderInSection
code
// Header
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = self.tableView.dequeueReusableHeaderFooterViewWithIdentifier("header") as! CollapsibleTableViewHeader
if sections.count == 0 {
self.tableView.userInteractionEnabled = false
header.cornerRadiusView.layer.borderWidth = 0.0
header.benefitAlertImage.hidden = true
header.benefitAlertText.hidden = true
header.amountLabel.hidden = true
}
else {
header.amountLabel.hidden = false
header.cornerRadiusView.layer.borderWidth = 1.0
self.tableView.userInteractionEnabled = true
header.titleLabel.text = sections[section].name
header.arrowImage.image = UIImage(named: "voucherDownArrow")
header.setCollapsed(sections[section].collapsed)
header.benefitDetailText2.text = sections[section].shortDesc
header.benefitDetailText3.text = sections[section].untilDate
header.section = section
header.delegate = self
if sections[section].collapsed == true {
header.benefitAlertImage.hidden = true
header.benefitAlertText.hidden = true
header.commerceIconsContainerView.hidden = true
for i in 0..<imagesArray.count {
imagesArray[i].image = UIImage(named: "")
}
}
else {
header.commerceIconsContainerView.hidden = false
if sections[section].items.count > 5 {
header.iconsViewHeightConstraint.constant = 74.0
}
else {
header.iconsViewHeightConstraint.constant = 38.0
}
if sections[section].isNearExpiration == true {
header.benefitAlertImage.hidden = false
header.benefitAlertText.hidden = false
}
else {
header.benefitAlertImage.hidden = true
header.benefitAlertText.hidden = true
}
}
}
return header
}