2017-03-20 4 views
1

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() 

     } 

} 

Antwort

0

Falls mit Ausfall- und erwarteter Höhe Verwendung automatischer Layout.

Beispiel benutzerdefinierte Zelle erstellen. Mit heightConstraint Ausgang zum Constraint.

class ExpandableCell: UITableViewCell { 

    @IBOutlet weak var heightConstraint: NSLayoutConstraint! 

    var isExpanded:Bool = false 
     { 
     didSet 
     { 
      if !isExpanded { 
       self.imgHeightConstraint.constant = 40.0 

      } else { 
       self.imgHeightConstraint.constant = 100.0 
      } 
     } 
    } 

} 

Set UITableViewAutomaticDimension für die tableView.

self.tableView.rowHeight = UITableViewAutomaticDimension 

implementieren Dann estimatedHeightForRowAt und erwartete Höhe zurück.

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { 

     return 40.0 

} 

Jetzt, wenn Sie Zelle wählen, implementieren Sie die Methode didSelectRowAt.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

        guard let cell = tableView.cellForRow(at: indexPath) as? ExpandableCell 

            else { return } 
         

        cell.isExpanded = !cell.isExpanded 

        self.tableView.beginUpdates() 

        self.tableView.endUpdates() 
} 
+0

meine Zelle Ansicht scheint jetzt nicht zu erweitern. –

+0

Wie Sie Constraint setzen? –

+0

Ich habe eine Beschränkung der Höhe zu meinem 'textView' hinzugefügt und dann in meine' CustomCell' gezogen. –

Verwandte Themen