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

时间:2017-03-16 16:59:34

标签: 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)
        }

}

1 个答案:

答案 0 :(得分:1)

在您拥有heightForRowAt

的班级中添加TableView
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath == selectedIndexPath {
      return CustomCell.expandedHeight
} 
else {
      return CustomCell.defaultHeight
  }
}