2017-03-20 5 views
1

私はtableViewのためにCustomCellになりました。タップしたときに伸び縮みします。また、展開状態のときはtextViewと表示されます。私は辞書の配列に自分の値を保存し、特定のセルに応じて私のtableViewにそれを印刷します。しかし、セル内の私のtextViewのサイズは固定されていますが、その特定の辞書の内容に応じてサイズを変更するにはどうすればよいですか?ここCustomCellの私のコードは次のとおりです。どのようにしてtextViewサイズの内容を動的に変更できますか?

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

そしてtableViewを持つクラスのコード:

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

     } 

} 

答えて

0

デフォルトと自動レイアウト期待の高さの使用を持つ場合。

例カスタムセルを作成します。制約への出口はheightConstraintです。

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 
      } 
     } 
    } 

} 

tableViewに設定さUITableViewAutomaticDimension

self.tableView.rowHeight = UITableViewAutomaticDimension 

次に、estimatedHeightForRowAtを実装し、予想される高さを返します。

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

     return 40.0 

} 

ここで、セル実装方法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

私のセルビューは現在展開していないようです。 –

+0

制約の設定方法は? –

+0

高さの制約を 'textView'に追加して、それを' CustomCell'にドラッグしました –

関連する問題