ios - 如何切换打开关闭uitableViewCell
问题描述
我有 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()
})
}
解决方案
推荐阅读
- objective-c - 使用带有 swift 包管理器的目标 c 库 (pop)
- node.js - 如何在 NodeJS 中从不同页面捕获 WebRTC 流
- java - Spring Data JDBC - 自动配置中的循环依赖
- typescript - RXJS publishLast 不发布最后发出的值?
- itext - 如何在 Java 中将 PowerPoint (.ppt) 转换为 PDF
- macos - 在 SwiftUI 中使用包装文本字段
- c - 为什么输出显示为“okNOT OK”?它应该显示“okOK”?请解释
- python - 如何在 Mxnet 框架中快速将列表转换为数组?
- c++ - 我的函数中的百分比计算不起作用
- java - Java: My while loop is crashing after the Switch case is done