如何根据文本中的内容使textView大小动态化?

时间:2017-03-20 14:25:29

标签: ios swift uitableview

我为我的CustomCell做了tableView,在点按时展开和收缩。当处于展开状态时,它显示textView。我将我的值保存在Dictionary数组中,然后根据特定单元格将其打印在tableView中。但是单元格中的textView大小已修复,如何根据特定字典的内容更改大小?这是我的CustomCell代码:

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

        }

}

1 个答案:

答案 0 :(得分:0)

如果使用默认和预期高度自动布局

示例创建自定义单元格。 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 } } } } 出口约束。

UITableViewAutomaticDimension

tableView设置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() }

{{1}}