如何删除并重新添加可滚动的 NSTextView 到视图?

问题描述

我有一个可滚动的 NSTextView,其中包含链接NSTableView 单元格的唯一文本。当我选择不同的 NSTableView 单元格并更新 NSTextView 以保持其新的 NSAttributedString 值时,撤消和重做功能变得混乱,所以我想我会尝试删除滚动视图并重新每次我想更新它时都添加它以希望重置撤消/重做链。

这是我目前的代码

var scrollView: NSScrollView = {
    let scroll = NSTextView.scrollableTextView()
    scroll.translatesAutoresizingMaskIntoConstraints = false
    return scroll
}()
var textView: NSTextView = {
    let view = NSTextView()
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}()

func updateView() { // my function to call when a new tableview cell is selected
    self.scrollView.removeFromSuperview()

    textView = scrollView.documentView as! NSTextView
    self.addSubView(scrollView)
    self.setUpConstraints() // my function containing layout constraints
}

但只有空格。这是我的 setUpConstraints() 函数

func setUpConstraints() { // I have other constraints in my program which relate to each other. All working.
    let scrollViewConstraints = [
        scrollView.topAnchor.constraint(equalTo: self.topAnchor,constant: 10),scrollView.leftAnchor.constraint(equalTo: self.leftAnchor,constant: 15),scrollView.rightAnchor.constraint(equalTo: self.rightAnchor,constant: -14),scrollView.bottomAnchor.constraint(lessthanorEqualTo: self.bottomAnchor,constant: -20)
    ]
    let allConstraints = [
        scrollViewConstraints
    ]
    for constraints in allConstraints {
        NSLayoutConstraint.activate(constraints)
    }
}

我还有其他视图,例如 NSTextField,我正在以完全相同的方式处理它们,它们都可以正常工作。问题是我可以把 self.addSubView(scrollView) 放在 viewDidMovetoSuperview 中就好了,但是当我把它放在我的 updateView 函数中时,它不起作用。 (仅供参考:updateView 是我的表视图的委托函数) 我也对我的撤消/重做问题的其他解决方案持开放态度。 提前致谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...