Ich bin eine CustomCell
für meine tableView
gemacht, die erweitert und kontrahiert, wenn es angezapft wird. Und wenn es sich im erweiterten Zustand befindet, zeigt es eine textView
. Ich speichere meine Werte in einem Array von Dictionary und drucke es dann in meiner tableView
nach bestimmten Zelle. Aber meine textView
in der Zelle hat die Größe festgelegt, wie ändere ich die Größe nach dem Inhalt des jeweiligen Wörterbuchs? Hier ist mein Code von CustomCell
:Wie kann ich eine TextView-Größe je nach Inhalt dynamisieren?
class CustomCell: UITableViewCell, BEMCheckBoxDelegate {
var isObserving = false;
class var expandedHeight: CGFloat { get { return 100 } }
class var defaultHeight: CGFloat { get { return 40 } }
func checkHeight() {
textView.isHidden = (frame.size.height < CustomCell.expandedHeight)
}
func watchFrameChanges() {
if !isObserving {
addObserver(self, forKeyPath: "frame", options: [NSKeyValueObservingOptions.new, NSKeyValueObservingOptions.initial], context: nil)
isObserving = true;
}
}
func ignoreFrameChanges() {
if isObserving {
removeObserver(self, forKeyPath: "frame")
isObserving = false;
}
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "frame"{
checkHeight()
}
}
Und der Code der Klasse, die tableView
hat:
var selectedIndexPath: IndexPath?
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCellID") as! CustomCell
cell.textView.text = arrOfDict[indexPath.row]["notesField"] as! String!
return (cell)
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath == selectedIndexPath
{
return CustomCell.expandedHeight
}
else {
return CustomCell.defaultHeight
}
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
(cell as! CustomCell).watchFrameChanges()
}
override func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
(cell as! CustomCell).ignoreFrameChanges()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
for cell in tableView.visibleCells as! [CustomCell] {
cell.ignoreFrameChanges()
}
}
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.reloadData()
}
}
meine Zelle Ansicht scheint jetzt nicht zu erweitern. –
Wie Sie Constraint setzen? –
Ich habe eine Beschränkung der Höhe zu meinem 'textView' hinzugefügt und dann in meine' CustomCell' gezogen. –