在NSTableView的NSCell中进行鼠标悬停检测? [英] mouseover detection in NSTableView's NSCell?

查看:44
本文介绍了在NSTableView的NSCell中进行鼠标悬停检测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在将鼠标悬停在表格视图的单元格上时更改其文本背景颜色,这类似于当鼠标悬停在标签名称上时,地址簿突出显示"联系人元素的标签的方式.但是我不知道如何完成...

I am wanting to change the text background color on a tableview's cell when it is hovered upon, similar to how AddressBook "highlights" the label of a contact's element when you mouseover the label names. However I cannot figure out how to accomplish...

检测特定NSCell上的鼠标悬停并...在检测到其悬停的单元格后,突出显示该单元格中的文本(而不突出显示整个行,就像用户选择了该行一样)

detecting a mouseover on a particular NSCell and... After detecting the cell his hovered upon, highlighting the text in that cell (not highlighting the entire row as if the user selected that row)

由于NSCell不是NSView的子类,因此这似乎是一项艰巨的任务.

As NSCell is not a subclass of NSView this seems to be a very difficult task.

任何对此示例或如何完成此操作的解释将不胜感激.

Any example of this or explanation on how this might be done would be greatly appreciated.

谢谢!

推荐答案

我实际上是使用另一种方法来使它工作的.我是从这里发布的示例中得到的... http://www.cocoadev.com/index.pl?NSTableViewRollover https://web.archive.org/web/20111013060111/http://cocoadev.com/index.pl?NSTableViewRollover

I actually got it working using another method. I got it from the example posted here... http://www.cocoadev.com/index.pl?NSTableViewRollover https://web.archive.org/web/20111013060111/http://cocoadev.com/index.pl?NSTableViewRollover

我不是使用NSCell的跟踪机制,而是在子类NSTableView中跟踪mouseEntered/mouseExited和mouseMoved.

Instead of using NSCell's tracking mechanism, I am tracking mouseEntered/mouseExited and mouseMoved within my subclassed NSTableView.

  1. 当调用tableview awakeFromNib方法时,我从tableview的可见部分创建了trackingRect

  1. When the tableview awakeFromNib method is called, I create a trackingRect from the visible portion of the tableview

我有一个BOOL ivar,当鼠标位于跟踪区域(mouseEntered)内时设置为YES,而当鼠标不在跟踪区域内时(mouseExited)设置为NO.

I have a BOOL ivar that is set to YES when the mouse is within the tracking area(mouseEntered) and NO when it is not (mouseExited)

在mouseMoved方法中,我确定鼠标光标所在的当前行并将其设置为NSInteger ivar,然后调用表视图的setNeedsDisplayInRect:传递鼠标所在行的rect.

Within the mouseMoved method, I determine the current row the mouse cursor is on and set it to an NSInteger ivar and then call the tableview's setNeedsDisplayInRect: passing the rect of the row that the mouse is on.

我还重写resetCursorRects来删除旧的跟踪rect并添加一个新的...在滚动表视图时将调用此方法,以便跟踪最新的可见rect.

I also override resetCursorRects to remove the old tracking rect and add a new one...this method is called when the tableview is scrolled upon so that it's tracking the latest visible rect.

最后,在我的表视图的委托中,我确定选定的行(通过从表视图的NSInteger ivar中检索行索引,并在当前绘制的单元格与鼠标光标所在的行.所有这些操作均通过委托方法完成:tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex

Finally in my tableview's delegate, I determine the selected row (by retrieving the row index from the NSInteger ivar of the table view and change the cell's text color (or anything you want) if the currently drawn cell matches the row the mouse cursor is on. All this is done in the delegate method: tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex

我希望这对其他人有帮助,因为这有点棘手.确保在加载视图时tableview是firstResponder可能也很重要,这只会使事情更加简化和简洁.

I hope this helps others, as this was a bit tricky. It is also probably important to make sure that tableview is the firstResponder when the view loads, just makes things a bit more streamlined and cleaner.

顺便说一句,有没有一种方法可以使视图中的特定控件始终是firstResponder,而没有其他可能成为firstResponder?甚至iPhone之类的方法... viewWillAppear方法也将有所帮助,因为我可以在每次查看该视图时都设置第一响应者...但是我在Mac上不知道这种方法.

Btw, is there a way to make a specific control in a view always be the firstResponder with nothing else possible as being the firstResponder? Even a method such as the iPhones... viewWillAppear method will help as I could set the first responder each time the view is visible...but i'm not aware of such a method on the Mac.

这篇关于在NSTableView的NSCell中进行鼠标悬停检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆