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()
})
}