swift - 将行高设置为0时,tableview最后一部分被打乱

Posted

技术标签:

【中文标题】swift - 将行高设置为0时,tableview最后一部分被打乱【英文标题】:tableview last section is scrambled when setting row height to 0 swift 【发布时间】:2020-04-02 07:19:13 【问题描述】:

我添加了一个箭头以在单击时折叠该部分,当我折叠最后一个部分时(例如,在 cellForRow 中将行高设置为 0,最后一个部分中的所有行都会混淆,如在下图:

如果我将行的高度设置为 0,任何人都可以提出为什么会发生这种情况的任何原因吗?

以下是相关代码:

viewForHeaderInSection:

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? 
        if section == 0 
            let profileCompletion = clientFields?.profileCompletion ?? 0
            headerView.profileComplete.text = "Profile".localized() + " \(profileCompletion)% " + "Complete".localized()
            headerView.profileProgress.progress = Float(profileCompletion) * 0.01
            headerView.profileProgress.progressTintColor = Constants.Client.getProfileCompletenessColor(profileCompletion)
            headerView.delegate = self
            headerView.section = section
            headerView.isOpen = self.sectionsStatuses[section].shouldDisplaySection

            return headerView
        

        else 
            let height = (section == 0) ? FIRST_HEADER_HEIGHT : HEADER_HEIGHT

            let nib = UINib(nibName: "ClientDetailsSectionHeaderCell", bundle: nil)
            guard let returnedView = nib.instantiate(withOwner: self, options: nil)[0] as? ClientDetailsSectionHeaderCell else 
                return UIView()
            

            returnedView.delegate = self
            returnedView.section = section
            returnedView.isOpen = self.sectionsStatuses[section].shouldDisplaySection
            returnedView.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: height)
            returnedView.heightConstraint.constant = height
            returnedView.mainView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            returnedView.mainView.backgroundColor = Constants.ParentForm.headerBgColor

            // Draw separators
            if DRAW_SECTION_SEPARATORS || DRAW_SECTION_TOP_SEPARATOR 
                returnedView.createTopSeparator(color: Constants.ParentForm.separatorColor)
            
            if DRAW_SECTION_SEPARATORS || DRAW_SECTION_BOTTOM_SEPARATOR 
                returnedView.createBottomSeparator(color: Constants.ParentForm.separatorColor)
            

            if isSectionTitleHidden == false 
                let xOffset = HEADER_X_OFFSET
                let yOffset = section == 0 ? FIRST_HEADER_Y_OFFSET : HEADER_Y_OFFSET
                returnedView.title.frame = CGRect(x: xOffset, y: yOffset, width: view.frame.size.width - xOffset, height: height - yOffset)
                returnedView.title.text = self.sectionsArray[section].uppercased().localized()
                returnedView.title.font = Constants.ParentForm.headerFont
                returnedView.title.textColor = Constants.ParentForm.headerTextColor
                returnedView.title.sizeToFit()
            

            return returnedView
        
    

heightForRow

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 
    if sectionsStatuses[indexPath.section].shouldDisplaySection 
        return super.tableView(tableView, heightForRowAt: indexPath)
    

    return CGFloat(0)

cellForRow

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
        if  self.sectionsArray[indexPath.section] != "ADDRESSES" 
            if let clientFields = self.clientFields 
                self.dataMap = Utils.convertObjectToMap(item: clientFields)

                let birthdayDate = clientFields.birthdayDateString
                self.dataMap["birthdayDate"] = birthdayDate
                let createdDate = clientFields.createdDateString
                self.dataMap["createdDate"] = createdDate
            
        
        else 
            if let clientAddresses = self.clientAddresses, clientAddresses.count > 0 
                self.dataMap = Utils.convertObjectToMap(item: clientAddresses[Int(floor(Double(indexPath.row) / 8.0))])
            
        

        return super.tableView(tableView, cellForRowAt: indexPath)
    

协议函数

func openCloseSection(openSection: Bool, section: Int) 
    self.sectionsStatuses[section].shouldDisplaySection = openSection
    self.tableView.reloadData()

ClientDetailsS​​ectionHeaderCell

import UIKit

protocol SectionViewerProtocol 
    func openCloseSection(openSection: Bool, section: Int)


class ClientDetailsSectionHeaderCell: UITableViewCell 
    @IBOutlet weak var mainView: UIView!
    @IBOutlet weak var heightConstraint: NSLayoutConstraint!
    @IBOutlet weak var accessoryButton: UIButton!
    @IBOutlet weak var title: UILabel!

    var section = 0
    var delegate: SectionViewerProtocol?

    var isOpen: Bool? 
        didSet 
            if let isOpen = self.isOpen 
                accessoryButton.setImage(UIImage(named: isOpen ? "fill588": "fill589"), for: .normal)
            
        
    

    override func awakeFromNib() 
        super.awakeFromNib()
    

    @IBAction func openCloseSection(_ sender: UIButton) 
        if let isOpen = self.isOpen 
            self.isOpen = !isOpen
            delegate?.openCloseSection(openSection: !isOpen, section: section)
        
    


    override func setSelected(_ selected: Bool, animated: Bool) 
        super.setSelected(selected, animated: animated)
    


【问题讨论】:

【参考方案1】:

尝试将单元格的clipsToBounds 或任何封闭的UIView 设置为true

【讨论】:

以上是关于swift - 将行高设置为0时,tableview最后一部分被打乱的主要内容,如果未能解决你的问题,请参考以下文章

xamarin.forms以编程方式将行高设置为0会导致不需要的空间

如何在C#中 禁止 dataGridView 自动添加行

如何设置表格中的行高以在 python-docx 中修复?

Swift - UIIMageView(Aspect Fill)和自动行高

Swift设置自动行高

CSS:理解和调整字体的前导/行高