在nstableview的Nstextfield中的角色位置

发布于 2025-01-25 01:11:16 字数 675 浏览 3 评论 0原文

我有一个基于n行和1列的基于视图的nstableview,并带有单个字体。当用户单击一排时,我需要将caret位置转换为字符索引中的数据填充该列的数据。我目前将Mousedown事件捕获在自定义Nstextfield中,但无法获得该位置的索引位置。

代码段:

    override func mouseDown(with event: NSEvent)
    {
        print (ME + ".\(#function)")

        let localPos = convert (event.locationInWindow, to: self)

        let cEditor = self.currentEditor() as? NSTextView
        
        let insertionPoint = cEditor?.characterIndexForInsertion(at: localPos)
                
        let location = cEditor?.selectedRange().location
    }

插入点等于416,这是显示数据的末端位置,位置为0,这两者都不反映Caret的位置。

任何帮助将不胜感激。在这一点上,我诉诸于戴围嘴,从我的愤怒中捕捉流口水。

XCode 12.3 Swift 5

I have a view based NSTableView of n rows and 1 column, with a monospaced font. When the user clicks within a row, I need to convert the caret position to a character index into the data populating that column. I currently trap the mouseDown event in a custom NSTextField, but am unable to obtain an index position of that location.

Code snippet:

    override func mouseDown(with event: NSEvent)
    {
        print (ME + ".\(#function)")

        let localPos = convert (event.locationInWindow, to: self)

        let cEditor = self.currentEditor() as? NSTextView
        
        let insertionPoint = cEditor?.characterIndexForInsertion(at: localPos)
                
        let location = cEditor?.selectedRange().location
    }

insertionPoint is equal to 416, which is the end position of the displayed data, location is 0, both of which don’t reflect the location of the caret.

Any help is greatly appreciated. At this point, I’ve resorted to wearing a bib to catch the drool from my exasperation.

Xcode 12.3
Swift 5

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

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

发布评论

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

评论(1

可爱暴击 2025-02-01 01:11:16

我的问题很大程度上是基于@Willeke的。更新的代码看起来像这样:

    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
    }

我将调用添加到super.mousedown()作为第一步,还将转换为:转换为nil的组件,这两者都导致了准确的位置索引。插入点为0,但位置是准确的,这可以解决我的问题。

我认为将IB用于最初的NstableView配置是一个错误,因为它给了我很多我不了解的设置,并且无法欣赏后果。但是 @Willeke的评论导致解决了这个特定问题的解决方案。不在自己的本地化项目中实现此功能也是一个错误,因为游戏中的代码数量太大,无法发布最小可重复的集合。

Based in large part on @Willeke, I have a solution to my problem. The updated code looks like this:

    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
    }

I added the call to super.mouseDown () as an initial step, and also changed the to: component of the convert to nil, both of which resulted in an accurate location index. The insertionPoint is 0, but location is accurate, and that solves my problem.

I think it was a mistake on my part to use IB for the initial NSTableView configuration as it gave me a lot of settings I didn't understand and couldn't appreciate the consequences. But @Willeke's comments led to the solution for this specific question. It was also a mistake to not implement this feature in its own localized project, as the amount of code in play was too large to post for a minimal reproducible set.

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