2017-03-16 73 views
0

我正在製作一個可摺疊的單元格,當單元格被點擊時展開和收縮,但是我沒有隱藏我的tableView,並給出了func checkHeight()中的約束條件。這是我的CustomCell類代碼:爲什麼我的textView沒有隱藏在我的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) 
     } 

} 
+0

你有沒有嘗試重新加載表視圖? –

+0

您需要重新加載單元格後點擊它,以便它可以重新加載新的高度。你可以使用/不使用動畫。 – creeperspeak

+0

@TaylorM實際上我的'TableView'不在這個類中,所以我不能在這個類中刷新它。 –

回答

1

在你的類添加heightForRowAt,你有TableView

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
if indexPath == selectedIndexPath { 
     return CustomCell.expandedHeight 
} 
else { 
     return CustomCell.defaultHeight 
    } 
} 
相關問題