美文网首页MacOSSwift
macOS开发之NSTableView重复点击

macOS开发之NSTableView重复点击

作者: chasitu | 来源:发表于2021-04-23 11:54 被阅读0次

首先我们来看看我们在使用NSTableview列表的问题

  • 首先NSTableview有一个点击回调的通知NSTableViewSelectionDidChangeNotification,但是这个通知只有选中变化的时候调用
  • tableViewSelectionDidChange:(NSNotification *)notification,这个就是上面的那个通知回调方法

解决:思路是判断点击的位置

  1. 自定义一个NSTableview,添加代理方法
@protocol SHCustomTableViewDelegate <NSObject>
@required
- (void)tableView:(NSTableView *)tableView didClickIndexPath:(NSIndexPath *)indexPath;
@end
@interface SHCustomTableView : NSTableView
@property (nonatomic , weak) id<SHCustomTableViewDelegate> extendedDelegate;
@end
  1. 获取鼠标点击的坐标进行判断
#import "SHCustomTableView.h"

@implementation SHCustomTableView

- (void)mouseDown:(NSEvent *)event
{
    NSPoint globalLocation = [event locationInWindow];
    NSPoint localLocation = [self convertPoint:globalLocation fromView:nil];
    NSInteger clickColumn = [self columnAtPoint:localLocation];
    NSInteger clickRow = [self rowAtPoint:localLocation];
    [super mouseDown:event];
    if (clickRow != -1) {
        [self.extendedDelegate tableView:self didClickIndexPath:[NSIndexPath indexPathForItem:clickRow inSection:clickColumn]];
    }
}

@end
  1. 使用
  • 遵守协议
@interface SHHomeWorkViewController ()<NSTableViewDelegate,
NSTableViewDataSource,
SHCustomTableViewDelegate>
  • 设置代理
  _tableView.extendedDelegate = self;
  • 实现方法
- (void)tableView:(NSTableView *)tableView didClickIndexPath:(NSIndexPath *)indexPath
{
    //do something
}

完成

相关文章

网友评论

    本文标题:macOS开发之NSTableView重复点击

    本文链接:https://www.haomeiwen.com/subject/hgrcrltx.html