我有一个名为 CollapsibleTableViewHeader
的 header ,它的高度是基于其中包含的 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 条件下,我需要返回标题的高度作为行的高度。因此,我将 header 中的所有元素相加并返回值 (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
}
最佳答案
您需要将在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]
}
关于ios - 用于具有动态高度的 View /标签的 Swift heightForRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44827455/