如果没有示例或 MCVE 项目,提供即插即用的解决方案将会很棘手。您链接到的主题和答案覆盖了 NSTableView 或 NSTableRowView 类,因此我可以为您提供一个通用的解决方案。 AppleScriptObjC 中的子类化可能有点麻烦,具体取决于您需要引用的内容,但相当简单。本质上,您是在常规类和应用程序之间放置一个自定义类,您可以在其中拦截各种标准方法调用。
对于基于单元格的表格视图示例,使用文件 > 新建 > 文件... 菜单项将新的空文件添加到您的项目中。 _file 的名称并不重要(例如TableViewHighlight.applescript 或其他),script 的名称将被 Xcode 使用。在这里,我使用 MyTableView 作为类名并从 AppDelegate 引用 tableView 插座属性:
script MyTableView -- the name of your custom class
property parent : class "NSTableView" -- the parent class to override
property tableView : a reference to current application's NSApp's delegate's tableView
property highlightColor : a reference to current application's NSColor's greenColor -- whatever
# set the row highlight color
on drawRow:row clipRect:clipRect
if tableView's selectedRowIndexes's containsIndex:row then -- filter as desired
highlightColor's setFill()
current application's NSRectFill(tableView's rectOfRow:row)
end if
continue drawRow:row clipRect:clipRect -- super
end drawRow:clipRect:
# set the highlight color of stuff behind the row (grid lines, etc)
on drawBackgroundInClipRect:clipRect
highlightColor's setFill()
current application's NSRectFill(clipRect)
continue drawBackgroundInClipRect:clipRect -- super
end drawBackgroundInClipRect:
end script
在界面编辑器中,使用Identity Inspector将表格视图的类设置为MyTableView类。最后,在您的表格视图设置中将其突出显示设置为无,因为它将由您的子类完成(再次假设tableView 出口连接到表格视图):
tableView's setSelectionHighlightStyle: current application's NSTableViewSelectionHighlightStyleNone
对于基于视图的表格视图示例,过程类似,但NSTableRowView是子类化的。这里我使用的脚本/类的名称是MyTableRowView:
script MyTableRowView -- the name of your custom class
property parent : class "NSTableRowView" -- the parent class to override
property highlightColor : a reference to current application's NSColor's redColor -- whatever
# draw the selected row
on drawSelectionInRect:dirtyRect
continue drawSelectionInRect:dirtyRect
highlightColor's setFill()
current application's NSRectFill(dirtyRect)
end drawSelectionInRect:
end script
在界面编辑器中,使用Attributes Inspector将表格视图的高亮设置为常规,并向表格视图的委托添加tableView:rowViewForRow:方法:
on tableView:tableView rowViewForRow:row
set rowIdentifier to "MyTableRow"
set myRowView to tableView's makeViewWithIdentifier:rowIdentifier owner:me
if myRowView is missing value then
set myRowView to current application's MyTableRowView's alloc's initWithFrame:current application's NSZeroRect
myRowView's setIdentifier:rowIdentifier
end if
return myRowView
end tableView:rowViewForRow:
当然还有其他选择,但这应该可以帮助您入门,并帮助翻译其中一些 Objective-C 答案/示例。