我正在制作一个可折叠的单元格,当单元格被点击时它会展开和收缩,但我无法使用 func checkHeight()
中的给定约束来隐藏我的 tableView。这是我的 CustomCell 类代码:
class CustomCell: UITableViewCell {
class var expandedHeight: CGFloat { get { return 170 } }
class var defaultHeight: CGFloat { get { return 44 } }
func checkHeight() {
textView.isHidden = (frame.size.height < CustomCell.expandedHeight)
}
override func awakeFromNib() {
super.awakeFromNib()
}
@IBOutlet weak var textView: UITextView!
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
这是我的 VC 代码,其中包含 TableView
var selectedIndexPath: IndexPath?
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let previousIndexPath = selectedIndexPath
if indexPath == selectedIndexPath {
selectedIndexPath = nil
} else {
selectedIndexPath = indexPath
}
var indexPaths : Array<IndexPath> = []
if let previous = previousIndexPath {
indexPaths += [previous]
}
if let current = selectedIndexPath {
indexPaths += [current]
}
if indexPaths.count > 0 {
tableView.reloadRows(at: indexPaths, with: UITableViewRowAnimation.automatic)
}
}
最佳答案
在你有 TableView
的类中添加 heightForRowAt
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath == selectedIndexPath {
return CustomCell.expandedHeight
}
else {
return CustomCell.defaultHeight
}
}
关于ios - 为什么我的 textView 没有隐藏在我的 CustomCell 类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42840371/