我有一个名为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,那么相同的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
}
答案 0 :(得分:1)
您需要将您在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]
}
答案 1 :(得分:1)
我不清楚你要做什么。您想根据其中的元素确定标题的高度应该是多少?如果是这样,那么你使用了错误的功能。你应该使用:
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
}
}
编辑: 刚看到你的编辑,仍然认为这是同一个问题。该函数正在创建一个标题视图,它的高度将通过调用heightForHeaderInSection而不是heightforRowatIndexPath从ViewController返回。