首页 > 解决方案 > 如何切换打开关闭uitableViewCell

问题描述

坍塌 崩溃2

我有 2 个单元格我想切换打开关闭,不幸的是它不起作用。当我第一次尝试打开应用程序时,单元格内的内容正在显示,因为在我的 tableView 单元格中,我默认使其不可见。当我尝试点击另一个单元格时,单元格内的内容仍然没有消失。我想要实现的是当我尝试打开应用程序时,所有单元格都处于关闭状态,当它被点击时它会展开。我怎样才能做到这一点?这是我的代码

// This is my subjectCell
    var selectedIndex: IndexPath = IndexPath(row: 0, section: 0)
    var isOpen: Bool?

    extension SubjectCell: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: DailyTableViewCell.cellID, for: indexPath) as! DailyTableViewCell
            cell.animate()
            return cell
        } else if indexPath.row == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: SubjectTableViewCell.cellID, for: indexPath) as! SubjectTableViewCell
            cell.animate()
            return cell
        }
        return UITableViewCell()
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectedIndex = indexPath

        tableView.beginUpdates()
        tableView.endUpdates()
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if selectedIndex.row == 0 {
            let cell = tableView.cellForRow(at: selectedIndex) as? DailyTableViewCell
            if isOpen == false {
                isOpen = true
                UIView.animate(withDuration: 0.7, delay: 0.3, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {
                    cell?.calendarView.alpha = 1
                    cell?.arrowImageView.image = #imageLiteral(resourceName: "up-chevron")
                })

                return 330
            } else {
                isOpen = false
                UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {
                    cell?.calendarView.alpha = 0
                    cell?.arrowImageView.image = #imageLiteral(resourceName: "down-chevron")
                })

                return 100
            }

        } else if selectedIndex.row == 1 {
            let cell = tableView.cellForRow(at: selectedIndex) as? SubjectTableViewCell
            if isOpen == false {
                isOpen = true
                UIView.animate(withDuration: 0.7, delay: 0.3, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {
                    cell?.tableView.alpha = 1
                    cell?.arrowImageView.image = #imageLiteral(resourceName: "up-chevron")
                })

                return 330
            } else {
                isOpen = false
                UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {
                    cell?.tableView.alpha = 0
                    cell?.arrowImageView.image = #imageLiteral(resourceName: "down-chevron")
                })

                return 100
            }
        }

        return 100
    }
}

// this is in my each tableViewCell
func animate() {
        UIView.animate(withDuration: 0.7, delay: 0.3, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {
            self.contentView.layoutIfNeeded()
        })
    }

标签: iosswiftuitableview

解决方案


推荐阅读