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

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

概述我正在使用 CollapsibleTableView from here并根据我的要求修改它以实现可折叠部分. Here is how it looks now. 由于根据UI设计我的部分有一个边框,我选择了部分标题作为我的UI元素,它在折叠和扩展模式下保存数据. 原因:我试过但是无法让它在下面解释的模型中运行 – **在标题标题中包含我的标题元素,并在单元格中包含每个项目的详细信息.默认情况下, 我正在使用 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 – Swift – tableView行高仅在滚动或切换展开/折叠后更新全部内容,希望文章能够帮你解决ios – Swift – tableView行高仅在滚动或切换展开/折叠后更新所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1001431.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存