gpt4 book ai didi

NSTableView Cell发起的Cocoa QuickLook

转载 作者:行者123 更新时间:2023-12-03 16:49:05 27 4
gpt4 key购买 nike

我有一个 NSTableView,其中包含 2 个不同的列 - 一个是显示文件图标的 NSImageCell,第二个是 NSTextFieldCell 的自定义子类,其中在文本右侧包含一个快速查看按钮。当我单击“快速查看”按钮时,将调用以下代码:

[[QLPreviewPanel sharedPreviewPanel] makeKeyAndOrderFront:nil];

这完成了它的工作,并显示空白的“快速查看”面板,上面写着“未选择任何项目”。在互联网上做了一些研究后,我实现了一个自定义 NSTableView 子类作为“快速查看”面板的委托(delegate)和数据源。我收到“快速查看”询问我是否想成为代表的通知,我回复"is"。即使我在 QLPreviewPanelDataSource 和 QLPreviewPanelDelegate 中实现了所有方法,但在运行时我在控制台上收到此错误:

2010-12-24 15:32:17.235 BackMeUp[4763:80f] clicked: ~/Desktop/HUDTape.mov
2010-12-24 15:32:17.489 BackMeUp[4763:80f] [QL] QLError(): -[QLPreviewPanel setDelegate:] called while the panel has no controller - Fix this or this will raise soon.
See comments in QLPreviewPanel.h for -acceptsPreviewPanelControl:/-beginPreviewPanelControl:/-endPreviewPanelControl:.
2010-12-24 15:32:17.490 BackMeUp[4763:80f] [QL] QLError(): -[QLPreviewPanel setDataSource:] called while the panel has no controller - Fix this or this will raise soon.
See comments in QLPreviewPanel.h for -acceptsPreviewPanelControl:/-beginPreviewPanelControl:/-endPreviewPanelControl:.
2010-12-24 15:32:17.491 BackMeUp[4763:80f] We can now receive QL Events.
2010-12-24 15:32:18.291 BackMeUp[4763:80f] -[NSPathStore2 stringValue]: unrecognized selector sent to instance 0x5ecb10
2010-12-24 15:32:18.292 BackMeUp[4763:80f] -[NSPathStore2 stringValue]: unrecognized selector sent to instance 0x5ecb10

并且“快速查看”面板没有显示,我觉得这很奇怪。上面的第一行只是我知道单元格已被单击。无论如何,这是自定义 TableView 子类的 .m 文件:

//
// BackupListTableView.m
// BackMeUp
//
// Created by Tristan Seifert on 12/24/10.
// Copyright 2010 24/7 Server. All rights reserved.
//

#import "BackupListTableView.h"


@implementation BackupListTableView

- (void) awakeFromNib {

}

// Quick Look Delegates

- (BOOL)acceptsPreviewPanelControl:(QLPreviewPanel *)panel;
{
[QLPreviewPanel sharedPreviewPanel].delegate = self;
[QLPreviewPanel sharedPreviewPanel].dataSource = self;

NSLog(@"We can now receive QL Events.");

return YES;
}

- (void)beginPreviewPanelControl:(QLPreviewPanel *)panel
{
// This document is now responsible of the preview panel
// It is allowed to set the delegate, data source and refresh panel.
[QLPreviewPanel sharedPreviewPanel].delegate = self;
[QLPreviewPanel sharedPreviewPanel].dataSource = self;
}

- (void)endPreviewPanelControl:(QLPreviewPanel *)panel
{
// This document loses its responsisibility on the preview panel
// Until the next call to -beginPreviewPanelControl: it must not
// change the panel's delegate, data source or refresh it.
return;
}

// Quick Look panel data source

- (NSInteger)numberOfPreviewItemsInPreviewPanel:(QLPreviewPanel *)panel
{
return 1;
}

- (id <QLPreviewItem>)previewPanel:(QLPreviewPanel *)panel previewItemAtIndex:(NSInteger)index
{
int selectedRow = [self selectedRow];


return [NSURL URLWithString:[[[self dataSource] tableView:self objectValueForTableColumn:fileColumn row:selectedRow] stringValue]];
}

// Quick Look panel delegate

- (BOOL)previewPanel:(QLPreviewPanel *)panel handleEvent:(NSEvent *)event
{
// redirect all key down events to the table view

return NO;
}

// This delegate method provides the rect on screen from which the panel will zoom.
- (NSRect)previewPanel:(QLPreviewPanel *)panel sourceFrameOnScreenForPreviewItem:(id <QLPreviewItem>)item
{

NSRect iconRect = [self rectOfColumn:1];
/*
// check that the icon rect is visible on screen
NSRect visibleRect = [self visibleRect];


// convert icon rect to screen coordinates
iconRect = [self convertRectToBase:iconRect];
iconRect.origin = [[self window] convertBaseToScreen:iconRect.origin];
*/
return iconRect;
}

// This delegate method provides a transition image between the table view and the preview panel
- (id)previewPanel:(QLPreviewPanel *)panel transitionImageForPreviewItem:(id <QLPreviewItem>)item contentRect:(NSRect *)contentRect
{
int selectedRow = [self selectedRow];

NSImage *fileIcon = [[NSWorkspace sharedWorkspace] iconForFile:[[[self dataSource] tableView:self objectValueForTableColumn:fileColumn row:selectedRow] stringValue]];

return fileIcon;
}

@end

感谢您的帮助。

最佳答案

该文档并不是最好的解决方案,因为它是 10.6 中添加的新功能。 (嗯,显然有类和协议(protocol)引用,但根据我的经验,我总是发现配套指南更有助于理解这些对象在现实场景中的使用方式)。

QLPreviewPanelController 协议(protocol)引用定义了 3 个方法:

QLPreviewPanelController Protocol Reference

The Quick Look preview panel shows previews for items provided by the first object in the responder chain that implements the methods in this protocol. You typically implement these methods in your window controller or delegate. You should never try to modify preview panel state if you’re not controlling the panel.

- (BOOL)acceptsPreviewPanelControl:(QLPreviewPanel *)panel;

- (BOOL)beginPreviewPanelControl:(QLPreviewPanel *)panel;

- (void)endPreviewPanelControl:(QLPreviewPanel *)panel;

我猜你的代码应该如下所示:

- (BOOL)acceptsPreviewPanelControl:(QLPreviewPanel *)panel
{
return YES;
}

除了返回 YES 之外,您不应该在该方法中执行任何操作。 acceptsPreviewPanelControl: 被发送到响应者链中的每个对象,直到返回 YES。通过返回 YES,该对象实际上成为“ Controller ”。从第一个方法返回 YES 后,在 Controller 对象上调用后两个方法。因此,您应该只在 beginPreviewPanelControl: 方法中设置委托(delegate)和数据源(此时您将被视为当前 Controller )。

- (void)beginPreviewPanelControl:(QLPreviewPanel *)panel
{

// This document is now responsible of the preview panel
// It is allowed to set the delegate, data source and refresh panel.

[QLPreviewPanel sharedPreviewPanel].delegate = self;
[QLPreviewPanel sharedPreviewPanel].dataSource = self;

NSLog(@"We can now receive QL Events.");
}

关于NSTableView Cell发起的Cocoa QuickLook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4528732/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com