0

collapse collapse2

I have 2 cell I want to toggle open close, unfortunately it not work. when I try to open the app for the first time a content inside cell is showing, since in my tableView cell i make it invisible by default. and when I try to tap the other cell the content inside the cell is still there not disappearing. what I want to achieve is when I try open the app all the cell in close state, and when it tapped it will expand. how can I do that? this is my code

// 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()
        })
    }
ferryawijayanto
  • 569
  • 4
  • 18
  • Where is your code that changes `isOpen` on tap? Also is `heightForRowAt` actually being called? – matt Apr 26 '20 at 10:54
  • Here’s a working example: https://github.com/mattneub/Programming-iOS-Book-Examples/blob/331902ea2f30deb452271bcd82931c98777729b2/bk2ch08p451dynamicTableContent2/AccordionCell/ViewController.swift – matt Apr 26 '20 at 10:58
  • @matt I update the question, the isOpen I make it an optional. and for the heightForRow I directly add in tableViewHeightForRow and the working example you give it to me, I try it before. but the problem is. the content inside table view is different since I have different tableViewCell – ferryawijayanto Apr 26 '20 at 11:20
  • it looks like a good idea, I'll try it @MacUserT :) – ferryawijayanto Apr 26 '20 at 11:26
  • @MacUserT can you help me a bit more hehe, when I tap the cell again, the cell not collapsing, I just did what you show me. it still not working – ferryawijayanto Apr 26 '20 at 12:23
  • 1
    you have to have a cell, which shows the user that he/she can tap to toggle. In tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) you call a method to collapse or expand the number of cells, for instance, func toggleExpansion(). In that method you start with setting the variable isExpanded = !isExpanded. Then if isExpanded = true you expand the array that you want to display in your cells and if false you reduce the array that provides the info for your cells. Don't forget to call tableView.reloadData() after you called func toggleExpansion(). – MacUserT Apr 26 '20 at 14:17

0 Answers0