ios – Swift – tableView行高仅在滚动或切换展开/折叠后更新

前端之家收集整理的这篇文章主要介绍了ios – Swift – tableView行高仅在滚动或切换展开/折叠后更新前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 CollapsibleTableView from here并根据我的要求修改它以实现可折叠部分. Here is how it looks now.

由于根据UI设计我的部分有一个边框,我选择了部分标题作为我的UI元素,它在折叠和扩展模式下保存数据.

原因:我试过但是无法让它在下面解释的模型中运行 –

**在标题标题中包含我的标题元素,并在单元格中包含每个项目的详细信息.默认情况下,该部分处于折叠状态.当用户点击标题时,切换单元格以显示.正如我所说,由于有一个边框需要显示给整个部分(点击标题及其单元格),我选择了section header作为我操作的UI元素.这是我的tableView代码

func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return sections.count 
    }

func tableView(tableView: UITableView,heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        switch indexPath.row {
        case 0:
            return sections[indexPath.section].collapsed! ? 0 : (100.0 + heightOfLabel2!)
        case 1:
            return 0
        case 2:
            return 0
        default:
            return 0
        }
    }


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
            header.titleLabel.text = "No_Vouchers".localized()
        }
        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)

            let stringRepresentation = sections[section].items.joinWithSeparator(",")

            header.benefitDetailText1.text = stringRepresentation
            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
            }
            else {
                if sections[section].isNearExpiration == true {
                    header.benefitAlertImage.hidden = false
                    header.benefitAlertText.hidden = false
                }
                else {
                    header.benefitAlertImage.hidden = true
                    header.benefitAlertText.hidden = true
                }
            }

            if appLanguageDefault == "nl" {
                self.totalAmountLabel.text = "€ \(sections[section].totalAvailableBudget)"
            }
            else {
                self.totalAmountLabel.text = "\(sections[section].totalAvailableBudget) €"
            }
        }

        return header
    }

切换折叠/展开的功能
我正在使用部分内“动态变化”UILabels的高度值,然后使用这些值来扩展边界(使用其layoutconstraint).

func toggleSection(header: CollapsibleTableViewHeader,section: Int) {
        let collapsed = !sections[section].collapsed

        header.benefitAlertImage.hidden = true
        header.benefitAlertText.hidden = true
        // Toggle collapse
        sections[section].collapsed = collapsed
        header.setCollapsed(collapsed)

        // Toggle Alert Labels show and hide
        if sections[section].collapsed == true {
            header.cornerRadiusViewBtmConstraint.constant = 0.0
            header.cornerRadiusViewTopConstraint.constant = 20.0
            header.benefitAlertImage.hidden = true
            header.benefitAlertText.hidden = true
        }
        else {

            heightOfLabel2 = header.benefitDetailText2.bounds.size.height

            if sections[section].isNearExpiration == true {
                header.benefitAlertImage.hidden = false
                header.benefitAlertText.hidden = false
                header.cornerRadiusViewBtmConstraint.constant = -100.0 - heightOfLabel2!
                header.cornerRadiusViewTopConstraint.constant = 10.0
                if let noOfDays = sections[section].daysUntilExpiration {
                    if appLanguageDefault == "nl" {

                        header.benefitAlertText.text = "(nog \(noOfDays) dagen geldig)"
                    }
                    else {
                        header.benefitAlertText.text = "(valable encore \(noOfDays) jour(s))"
                    }
                }                
            }
            else {
                header.cornerRadiusViewBtmConstraint.constant = -80.0 - heightOfLabel2!
                header.cornerRadiusViewTopConstraint.constant = 20.0
                header.benefitAlertImage.hidden = true
                header.benefitAlertText.hidden = true
            }
        }

        // Adjust the height of the rows inside the section
        tableView.beginUpdates()
        for i in 0 ..< sections.count {
            tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: i,inSection: section)],withRowAnimation: .Automatic)
        }
        tableView.endUpdates()
    }

问题:
根据某些条件,我需要在第一次启动视图时默认扩展此表视图中的几个节头.当我计算标签的高度并使用高度来设置边框的顶部和底部约束时,根据设计显示扩展的部分标题变得很困难.

由于我的UILabel的高度默认为21,因此内容离开边界.

更新:行高仅在我滚动视图或在崩溃/展开之间切换后才会更改

问题:
如何在第一次启动视图时计算我的Section标题中出现的UILabel的高度? (这意味着,在我的REST调用完成后,获取数据然后我需要获得UIlabel高度).

目前,我使用的是heightOfLabel2 = header.benefitDetailText2.bounds.size.height

(要么)

有没有更好的方法来实现这一目标?

提前致谢!

解决方法

您可以尝试使用String扩展来计算边界矩形

extension String {
    func height(withConstrainedWidth width: CGFloat,font: UIFont) -> CGFloat {
        let constraintRect = CGSize(width: width,height: .greatestFiniteMagnitude)
        let boundingBox = self.boundingRect(with: constraintRect,options: .usesLineFragmentOrigin,attributes: [NSFontAttributeName: font],context: nil)

        return boundingBox.height
    }
}

资料来源:Figure out size of UILabel based on String in Swift

猜你在找的iOS相关文章