ios - 为什么我的 textView 没有隐藏在我的 CustomCell 类中

标签 ios swift uitableview

我正在制作一个可折叠的单元格,当单元格被点击时它会展开和收缩,但我无法使用 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/

相关文章:

iphone - NSMutableArray 没有添加所有项目,只是最后一个 - Iphone/IOS/xcode

ios - 如何将信息从一个 IBAction 传输到另一个 IBAction

ios - swift 核心数据 : How to fetch all items in array?

来自 Firebase 的 Swift 4 展开字典

ios - 无法解析 RealmSwift 中的格式字符串

ios - YTPlayerView ipad全屏播放视频

ios - 如何让 UITableViewCell 图像更新为下载的图像而无需滚动 UITableView

iOS - 如何设置 View 宽度等于 TableView 分隔符宽度

ios - UITableViewCell setSelected :animated called twice on iPad

iOS蓝牙热敏票据打印机,支持哪款?