将行高设置为0 swift时,视图的最后部分将被加扰

问题描述 投票:1回答:1

[单击时,我添加了一个箭头来折叠该部分,当我折叠最后一个部分时(例如,在cellForRow中将行高设置为0,则最后一个部分中的所有行都会混合在一起,如图中所示)下方:enter image description here

如果我将行的高度设置为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)
    }

}
uitableview swift4 reloaddata uitableviewsectionheader heightforrowatindexpath
1个回答
0
投票

尝试设置单元格的clipsToBounds或将UIView包含在true中的任何内容。

© www.soinside.com 2019 - 2024. All rights reserved.