我有一个名为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
}
}
在我的其他条件下,我需要将标题的高度作为行高的高度返回。因此,我将标题内的所有元素加起来并返回值(CGFLOAT(。
返回高度按预期返回,但问题是将相同的高度应用于所有单元格。例如如果返回的高度值为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
}
您需要为在cellForRow
方法中计算的每个单元格存储在数组中,然后加载然后加载heightForRowAtIndexPath
函数
例如:
var heights = [Int]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! Cell
heights.append(calculationResult)
return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return heights[indexPath.row]
}
我不清楚您要做什么。您想根据其中的元素确定标头的高度应为什么?如果是这样,则使用错误的功能。您应该使用:
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if (isSectionCollapsed) {
return 0
} else {
let sum = subView1.height + subView2.height + subView3.height
return sum
}
}
这就是为什么您所有的行都将其高度设置为150的原因。如果您试图隐藏一行,则您还需要对您列出的功能中的行进行类似的检查:
override func tableView(_ tableView: UITableView, heightforRowatIndexPath section: Int) -> CGFloat {
if (isSectionCollapsed) {
return 0
} else {
// return row height for this tableViewCell
}
}
编辑:只是看到了您的编辑,但仍然认为这是同一问题。该功能正在创建标头视图,并且该功能将通过调用到高度forheaderinsection而不是高度forrowatIndExpath。