有什么方法可以将mouseOver事件添加到可可中的tableView吗?

Edw*_*ard 2 macos cocoa objective-c nstableview

我想让我的tableView像这样:
当鼠标滑过某一行时,该行将被突出显示,就像按钮的mouseOver事件一样

lon*_*kai 5

基于提示,我花了一些时间进行处理。

它对我有用,如果我错了,请纠正我。

在macOS 10.12.2和Xcode 8.2.1上使用Swift 3.0.2进行了测试

//
// Created by longkai on 30/12/2016.
// Copyright (c) 2016 xiaolongtongxue.com. All rights reserved.
//

import Cocoa

class InboxTableCellView: NSTableCellView {
    // MARK: - Outlets
    @IBOutlet weak var title: NSTextField!
    @IBOutlet weak var sender: NSTextField!
    @IBOutlet weak var time: NSTextField!
    @IBOutlet weak var snippet: NSTextField!

    // MARK: - Mouse hover
    deinit {
        removeTrackingArea(trackingArea)
    }

    private var trackingArea: NSTrackingArea!

    override func awakeFromNib() {
        super.awakeFromNib()
        self.trackingArea = NSTrackingArea(
            rect: bounds,
            options: [NSTrackingAreaOptions.activeAlways, NSTrackingAreaOptions.mouseEnteredAndExited,/* NSTrackingAreaOptions.mouseMoved */],
            owner: self,
            userInfo: nil
        )
        addTrackingArea(trackingArea)
    }

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)

        NSColor(red: 0.96, green: 0.96, blue: 0.96, alpha: 1.00).set()

        // mouse hover
        if highlight {
            let path = NSBezierPath(rect: bounds)
            path.fill()
        }

        // draw divider
        let rect = NSRect(x: 0, y: bounds.height - 2, width: bounds.width, height: bounds.height)
        let path = NSBezierPath(rect: rect)
        path.fill()
    }

    private var highlight = false {
        didSet {
            setNeedsDisplay(bounds)
        }
    }

    override func mouseEntered(with event: NSEvent) {
        super.mouseEntered(with: event)
        if !highlight {
            highlight = true
        }
    }

    override func mouseExited(with event: NSEvent) {
        super.mouseExited(with: event)
        if highlight {
            highlight = false
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


geo*_*war 1

(忽略“鼠标悬停是糟糕的 GUI”说教(无论如何你都会忽略它\xe2\x80\xa6 ;-))

\n\n
#import "MoTableView.h"\n\n@implementation MoTableView\n{\n    NSUInteger mouseRow;\n    NSRect mouseRowFrame;\n}\n\n- (id)initWithFrame:(NSRect)frame\n{\n    self = [super initWithFrame:frame];\n    if (self) {\n        // Initialization code here.\n        mouseRow = -1;\n    }\n    return self;\n}\n\n- (void)awakeFromNib\n{\n    [self.window setAcceptsMouseMovedEvents:YES];\n}\n\n- (void)drawRect:(NSRect)dirtyRect\n{\n    [super drawRect:dirtyRect];\n\n    // Drawing code here.\n    [[NSColor redColor] set];\n    NSLogDebug(@"mouseRowFrame: %@", NSStringFromRect(mouseRowFrame));\n    NSFrameRectWithWidth(mouseRowFrame, 2.);\n}\n\n- (void)mouseMoved:(NSEvent *)theEvent\n{\n    NSPoint mouseLocation = [theEvent locationInWindow];\n    NSPoint viewLocation = [self convertPoint:mouseLocation fromView:nil] ;\n    NSInteger row = [self rowAtPoint:viewLocation];\n    if (row != mouseRow) {\n        mouseRowFrame = [self rectOfRow:row];\n        [self setNeedsDisplay];\n        mouseRow = row;\n    }\n}\n\n@end\n
Run Code Online (Sandbox Code Playgroud)\n