在NstableView中选择时Nstextfield对齐

发布于 2025-01-26 03:09:46 字数 4057 浏览 5 评论 0原文

我有一个编程创建的NstableView,其中n行和1列。嵌入式文本字段是可选的,而不是可编辑的。我能够观察文本字段中的Mousedown事件,但是如您在图像中所看到的,所选行是重新定位并包装了单词。我怀疑这可能与现场编辑有关,但是我的Macos Odyssey证明了我无数的错误时期。

我的问题:我需要做些什么来维护所选行的布局以与表中的其他行保持一致?

更新:添加textfield.cell.wraps = false forse tobleview委托消除了单词包裹,但仍然存在所选文本字段的问题。

代码片段:
从表委托:

    func tableView (_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView?
    {
        let text = dataArray [row]
        var v = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier.init(rawValue: "TableColumn"), owner: self) as? MyTextFieldExt
        if v == nil
        {
            v = MyTextFieldExt ()
            v?.identifier = NSUserInterfaceItemIdentifier(rawValue: "TableColumn")
            v?.maximumNumberOfLines = 1
            v?.autoresizingMask = [.width]
            v?.setContentHuggingPriority(NSLayoutConstraint.Priority(rawValue: 251), for: .horizontal)
            v?.translatesAutoresizingMaskIntoConstraints = false
            
            v?.isEditable = false
            v?.isSelectable = true
            
            v?.cell?.wraps = false
        }
        
        v!.stringValue = text
        v!.font = NSFont.monospacedSystemFont(ofSize: 10, weight: .regular)
        v!.frame = CGRect(x: 0, y: 0, width: 3000, height: 0)

        return v!
    }

从捕获Mousedown的文本字段(自定义):

    override func mouseDown(with event: NSEvent)
    {
        super.mouseDown(with: event)

        let cEditor = self.currentEditor() as? NSTextView
        let localPos = convert (event.locationInWindow, to: nil)

        let location = cEditor?.selectedRange().location

        if let r = cEditor?.selectedRange()
        {
            self.select(withFrame: self.frame, editor: cEditor!, delegate: self, start: r.location, length: r.length+10)
        }

    }

从创建scrollview,表观和列的视图控制器:

    func setupTableView ()
    {
        let tableView = MyTableViewExt ()
        tableView.selectionHighlightStyle = .none
        
        tableView.headerView = nil
        tableView.columnAutoresizingStyle = .lastColumnOnlyAutoresizingStyle
        tableView.autoresizesSubviews = true
        tableView.autoresizingMask = [.width, .height]
        
        let column = NSTableColumn ()
        column.identifier = NSUserInterfaceItemIdentifier(rawValue: "TableColumn")
        column.width = 426
        column.minWidth = 40
        column.maxWidth = 1000
        column.resizingMask = [.autoresizingMask, .userResizingMask] // verify in debugger
        
        tableView.addTableColumn(column)

        tableView.delegate = self
        tableView.dataSource = self
        
        let scrollView = NSScrollView (frame: self.view.bounds)
        scrollView.autoresizesSubviews = true
        scrollView.autoresizingMask = [.height, .width]
        scrollView.translatesAutoresizingMaskIntoConstraints = false        
        scrollView.hasHorizontalScroller = true
        scrollView.hasVerticalScroller = true
        
        self.view.addSubview(scrollView)
        
        self.view.addConstraint(NSLayoutConstraint (item: self.view, attribute: .trailing, relatedBy: .equal, toItem: scrollView, attribute: .trailing, multiplier: 1.0, constant: 20))
        self.view.addConstraint(NSLayoutConstraint (item: scrollView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1.0, constant: 20))
        self.view.addConstraint(NSLayoutConstraint (item: self.view, attribute: .bottom, relatedBy: .equal, toItem: scrollView, attribute: .bottom, multiplier: 1.0, constant: 20))
        self.view.addConstraint(NSLayoutConstraint (item: scrollView, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1.0, constant: 20))
        
        scrollView.documentView = tableView
    }

I have a programmatically created NSTableView with n rows and 1 column. The embedded text field is selectable, and not editable. I am able to observe the mouseDown event in the text field, but as you can see in the image, the selected row is repositioned and word wrapped. I suspect this may be related to the field editor, but my MacOS odyssey has proven me wrong innumerable times.

My question: What do I need to do to maintain the layout of the selected row to be consistent with the other rows in the table?

Update: adding a textfield.cell.wraps = false to the tableview delegate eliminated the word wrap, but still have the issue with the selected text field indented.

enter image description here

Code snippets:
From the table delegate:

    func tableView (_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView?
    {
        let text = dataArray [row]
        var v = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier.init(rawValue: "TableColumn"), owner: self) as? MyTextFieldExt
        if v == nil
        {
            v = MyTextFieldExt ()
            v?.identifier = NSUserInterfaceItemIdentifier(rawValue: "TableColumn")
            v?.maximumNumberOfLines = 1
            v?.autoresizingMask = [.width]
            v?.setContentHuggingPriority(NSLayoutConstraint.Priority(rawValue: 251), for: .horizontal)
            v?.translatesAutoresizingMaskIntoConstraints = false
            
            v?.isEditable = false
            v?.isSelectable = true
            
            v?.cell?.wraps = false
        }
        
        v!.stringValue = text
        v!.font = NSFont.monospacedSystemFont(ofSize: 10, weight: .regular)
        v!.frame = CGRect(x: 0, y: 0, width: 3000, height: 0)

        return v!
    }

From the text field (custom) that traps the mouseDown:

    override func mouseDown(with event: NSEvent)
    {
        super.mouseDown(with: event)

        let cEditor = self.currentEditor() as? NSTextView
        let localPos = convert (event.locationInWindow, to: nil)

        let location = cEditor?.selectedRange().location

        if let r = cEditor?.selectedRange()
        {
            self.select(withFrame: self.frame, editor: cEditor!, delegate: self, start: r.location, length: r.length+10)
        }

    }

From the view controller that creates the scrollview, table view, and column:

    func setupTableView ()
    {
        let tableView = MyTableViewExt ()
        tableView.selectionHighlightStyle = .none
        
        tableView.headerView = nil
        tableView.columnAutoresizingStyle = .lastColumnOnlyAutoresizingStyle
        tableView.autoresizesSubviews = true
        tableView.autoresizingMask = [.width, .height]
        
        let column = NSTableColumn ()
        column.identifier = NSUserInterfaceItemIdentifier(rawValue: "TableColumn")
        column.width = 426
        column.minWidth = 40
        column.maxWidth = 1000
        column.resizingMask = [.autoresizingMask, .userResizingMask] // verify in debugger
        
        tableView.addTableColumn(column)

        tableView.delegate = self
        tableView.dataSource = self
        
        let scrollView = NSScrollView (frame: self.view.bounds)
        scrollView.autoresizesSubviews = true
        scrollView.autoresizingMask = [.height, .width]
        scrollView.translatesAutoresizingMaskIntoConstraints = false        
        scrollView.hasHorizontalScroller = true
        scrollView.hasVerticalScroller = true
        
        self.view.addSubview(scrollView)
        
        self.view.addConstraint(NSLayoutConstraint (item: self.view, attribute: .trailing, relatedBy: .equal, toItem: scrollView, attribute: .trailing, multiplier: 1.0, constant: 20))
        self.view.addConstraint(NSLayoutConstraint (item: scrollView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1.0, constant: 20))
        self.view.addConstraint(NSLayoutConstraint (item: self.view, attribute: .bottom, relatedBy: .equal, toItem: scrollView, attribute: .bottom, multiplier: 1.0, constant: 20))
        self.view.addConstraint(NSLayoutConstraint (item: scrollView, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1.0, constant: 20))
        
        scrollView.documentView = tableView
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

永不分离 2025-02-02 03:09:46

因此,通过修改文本字段Mousedown逻辑中的代码来解决我的问题。校正的代码看起来像这样,并且在设置选定范围的设置时会发生变化:

    override func mouseDown(with event: NSEvent)
    {
        super.mouseDown(with: event)

        let cEditor = self.currentEditor() as? NSTextView
        let localPos = convert (event.locationInWindow, to: nil)
        let insertionPoint = cEditor?.characterIndexForInsertion(at: localPos)
        let location = cEditor?.selectedRange().location

        if let r = cEditor?.selectedRange()
        {            
//          self.select(withFrame: self.frame, editor: cEditor!, delegate: self, start: r.location, length: r.length+10) <-- Source of problem
            cEditor?.setSelectedRange(NSMakeRange(r.location, r.length+10)) <-- Solution to problem
        }
    }

而且要明确,不需要textfield.cell?cell?.wraps = false i在评论中做出的陈述是不正确的。在我的表观视图委托中需要该行以避免包裹一词。

So my problem was resolved by modifying the code in the text field mouseDown logic. The corrected code looks like this, with a change in setting the selected range:

    override func mouseDown(with event: NSEvent)
    {
        super.mouseDown(with: event)

        let cEditor = self.currentEditor() as? NSTextView
        let localPos = convert (event.locationInWindow, to: nil)
        let insertionPoint = cEditor?.characterIndexForInsertion(at: localPos)
        let location = cEditor?.selectedRange().location

        if let r = cEditor?.selectedRange()
        {            
//          self.select(withFrame: self.frame, editor: cEditor!, delegate: self, start: r.location, length: r.length+10) <-- Source of problem
            cEditor?.setSelectedRange(NSMakeRange(r.location, r.length+10)) <-- Solution to problem
        }
    }

And just to be clear, the statement of not requiring the textfield.cell?.wraps = false I made in a comment is incorrect. That line is needed in my table view delegate to avoid the word wrap.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文