0

我有一个名为的标题,它的高度是动态的,CollapsibleTableViewHeader基于它包含在其中。UILabelsUIViews

所以,我在下面有这个功能 -

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
    }
4

2 回答 2

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
    }
 }

编辑:刚刚看到你的编辑,仍然认为这是同一个问题。该函数正在创建一个标题视图,其高度将从 ViewController 通过调用 heightForHeaderInSection 而不是 heightforRowatIndexPath 返回。

于 2017-06-29T14:42:43.493 回答
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]
}
于 2017-06-29T14:14:27.980 回答