了解NStableView的下降 [英] Understand drop in NStableView

查看:56
本文介绍了了解NStableView的下降的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有数据源和IB中绑定的NStableview.DataSource对象从NSOpenPanel Action获取它们的值.然后,一个函数从openPanel接收URLS,并很好地完成了肮脏的工作.因此,我将要实现的是通过Drag操作为openPanel提供替代方案.真心的,我需要告诉我我对拖放没有足够的技巧,并且在阅读和阅读了有关它的文档之后,我手中还是有几只苍蝇....目前,我可以将文件拖放到tableview中,正确接收文件url,在datasorce数组中加载值,但是我无法在tableview中放入行.再说一遍,我想通过拖动openPanel的替代方法来实现,因此任何建议在很大程度上都值得赞赏.谢谢,很抱歉,很长的代码

I have a NStableview with datasource and bindings in IB. DataSource object get they value from NSOpenPanel Action. Then, a function receive URLS from openPanel, and do the dirty work very well. So, what i would achieve is offer an alternative to openPanel via Drag operation. Sincerely i need to tell that i don't have quite skill about drag and drop, and after read and read documentation about it, still have o couple of fly in my hand.... At the moment i can drop file in tableview, receive the file url corrctly, load value in datasorce array, but i can't put row in tableview. Repeat i would like achive via drag an alternative to openPanel so any suggestion is largely appreciated. thank you and sorry for long code

我的代码:

//step 1
- (void)awakeFromNib {    
  [_tableView registerForDraggedTypes:[NSArray arrayWithObject:(NSURL*)kUTTypeFileURL]];
}

//2
- (BOOL)tableView:(NSTableView *)tableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes     toPasteboard:(NSPasteboard*)pboard
{
NSLog(@"write rows with indexes"); // no log output
return YES;
}
 //3
- (NSDragOperation)tableView:(NSTableView *)_tableView validateDrop:(id < NSDraggingInfo >)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)operation
{
  //get the file URLs from the pasteboard
NSPasteboard* pb = info.draggingPasteboard;

//list the file type UTIs we want to accept
NSArray* acceptedTypes = [NSArray arrayWithObject:(NSString*)kUTTypeMP3];

NSArray* urls = [pb readObjectsForClasses:[NSArray arrayWithObject:[NSURL class]]
                                  options:[NSDictionary dictionaryWithObjectsAndKeys:
                                           [NSNumber numberWithBool:YES],NSPasteboardURLReadingFileURLsOnlyKey,
                                           acceptedTypes,   NSPasteboardURLReadingContentsConformToTypesKey,
                                           nil]];

//only allow drag if there is exactly one file
if(urls.count != 1)
return NSDragOperationNone;

NSLog(@"ArrayDrag is %@", [urls description]);
NSLog(@"ArrayDrag is %lu", (unsigned long)[urls count]);

if ( [[pb types] containsObject:NSURLPboardType] ) {

NSURL *fileURL = [NSURL URLFromPasteboard:pb];
NSLog(@"fileURL Drop %@", fileURL); // url log OK!!!

// [self audioSetup:fileURL]; // function audioSetup receive the url ok.

}


return NSDragOperationCopy; // how to deal this return?
}


- (BOOL)tableView:(NSTableView *)_tableView acceptDrop:(id < NSDraggingInfo >)info    row:(NSInteger)row dropOperation:(NSTableViewDropOperation)operation {

 //Get the files from the drop
 NSArray * files = [[info draggingPasteboard] propertyListForType:NSURLPboardType];

 NSLog(@"FILES files is: %@", files);
 NSLog(@"FILES COUNT is: %lu", (unsigned long)files.count); // Count return is 2! ?

 NSLog(@"FILES INDEX 0 is: %@", [files objectAtIndex:0]); // is right url
 NSLog(@"FILES INDEX 1 is: %@", [files objectAtIndex:1]); // is "" ?!?

 NSURL *urlFromBP = [files objectAtIndex:0]; // 
 [self audioSetup:urlFromBP];

 NSLog(@"FILES 2 files is: %@", [files description]);    

 if([files count] > 0) return YES;
 return NO;
 }

//在这一点上似乎还可以,但是在电视中拖动文件后,我得到了...

//at this point it seems ok but after dragged a file in TV i get this...

FILES COUNT is: 2
2014-07-10 14:18:46.339 ToolSet[1618:303] FILES INDEX 0 is: file:///Users/s...x/Desktop /NUOVE%20TRAXSOURCE /Boston%20Rodriguez,%20Cherie%20Mathieson%20-%20Lost%20in%20Space%20(DJ%20Spinna%20Galactic%20Soul%20    Remix).mp3
2014-07-10 14:18:46.339 ToolSet[1618:303] FILES INDEX 1 is: 

2014-07-10 14:18:46.340 ToolSet[1618:303] myAudioUrl is: (
"file:///Users/s....x/Desktop/NUOVE%20TRAXSOURCE /Boston%20Rodriguez,%20Cherie%20Mathieson%20-%20Lost%20in%20Space%20(DJ%20Spinna%20Galactic%20Soul%20     Remix).mp3"
 )
2014-07-10 14:18:46.340 ToolSet[1618:303] myAudioUrl CouNt: 1
2014-07-10 14:18:46.340 ToolSet[1618:303] tuuti gli audioURL0: file:///Users/s.....x/Desktop /NUOVE%20TRAXSOURCE  /Boston%20Rodriguez,%20Cherie%20Mathieson%20-%20Lost%20in%20Space%20(DJ%20Spinna%20Galactic%20Soul%20    Remix).mp3

Up to here everything seems to be fine

2014-07-10 14:18:46.341 ToolSet[1618:303] -[__NSCFString baseURL]: unrecognized selector sent to  instance 0x600000148140
2014-07-10 14:18:46.341 ToolSet[1618:303] *** Canceling drag because exception   'NSInvalidArgumentException' (reason '-[__NSCFString baseURL]: unrecognized selector sent to     instance 0x600000148140') was raised during a dragging session

我需要了解两个错误从何而来!

please i need to understand from where come the two error!

推荐答案

经过深入调查,我找到了实现目标的正确方向.注意:Drop URL一次只能拖动一个文件.

After a deep investigation i have found the right direction for achieve my gol. Note: Drop URL allow drag only one file at time.

// declare registerForDraggedTypes

- (void)awakeFromNib {
[_tableView registerForDraggedTypes:[NSArray arrayWithObject:(NSURL*)kUTTypeFileURL]];
}

// method for readableTypesForPasteboard

+ (NSArray*)
readableTypesForPasteboard: (NSPasteboard*) inPasteboard
{
static NSArray* types = nil;
if (types == nil)
{
    types = [NSArray arrayWithObjects: [NSArray arrayWithObject:(NSURL*)kUTTypeFileURL], nil];
}

return types;
}

// Boolean method

- (BOOL)tableView:(NSTableView *)tableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes   toPasteboard:(NSPasteboard*)pboard
{
NSLog(@"write rows with indexes");
return YES;
}

//Validate drop in TV

- (NSDragOperation)tableView:(NSTableView *)_tableView validateDrop:(id < NSDraggingInfo >)info  proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)operation
{
//get the file URLs from the pasteboard
NSPasteboard* pb = info.draggingPasteboard;

//list the file type UTIs we want to accept
NSArray* acceptedTypes = [NSArray arrayWithObject:(NSString*)kUTTypeMP3];

NSArray* urls = [pb readObjectsForClasses:[NSArray arrayWithObject:[NSURL class]]
                                  options:[NSDictionary dictionaryWithObjectsAndKeys:
                                           [NSNumber numberWithBool:YES],NSPasteboardURLReadingFileURLsOnlyKey,
                                           acceptedTypes, NSPasteboardURLReadingContentsConformToTypesKey,
                                           nil]];

//only allow drag if there is exactly one file
if(urls.count != 1)

    //if(urls.count != 1)
    return NSDragOperationNone;

NSLog(@"ArrayDrag is %@", [urls description]);
NSLog(@"ArrayDrag is %lu", (unsigned long)[urls count]);

if ( [[pb types] containsObject:NSURLPboardType] ) {

    NSURL *fileURL = [NSURL URLFromPasteboard:pb];
    NSLog(@"fileURL Drop %@", fileURL);

}

return NSDragOperationCopy;
}

// Accept drop in TV

- (BOOL)tableView:(NSTableView *)_tableView acceptDrop:(id < NSDraggingInfo >)info  row:(NSInteger)row dropOperation:(NSTableViewDropOperation)operation {
NSPasteboard *pboard = [info draggingPasteboard];

if ( [[pboard types] containsObject:NSURLPboardType] ) {

    NSURL *fileURL = [NSURL URLFromPasteboard:pboard];

    NSLog(@"fileurl in PASTEBOARD %@", fileURL);
    [self getURLfromPb:fileURL];

}

return YES;

if([files count] > 0) return YES;

return NO;

}

这篇关于了解NStableView的下降的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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