我有一个名为的标题,它的高度是动态的,CollapsibleTableViewHeader
基于它包含在其中。UILabels
UIViews
所以,我在下面有这个功能 -
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
}
}
在我的 else 条件下,我需要将标题的高度作为行的高度返回。因此,我将标题中的所有元素相加并返回值(CGFloat)。
高度按预期返回,但问题是相同的高度应用于所有单元格。例如,如果返回的高度值为 150.0,则相同的 150.0 将应用于所有单元格,而不管它们各自的高度。
如何获得每个单元格的特定高度?indexPath
是一件关键的事情,但我不知道如何在这里使用它。
抱歉,如果问题很愚蠢!请帮忙
PS:我automaticDimension
已经尝试过了,但这无论如何都无济于事,因为我将这些单元格作为折叠/可扩展单元格。
编辑 1:添加viewForHeaderInSection
代码
// 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
}