Mac下app的table拖放实现s

<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);">Mac下的mp3播放器,同步显示lrc歌词。因需要将Finder中的歌曲拖放入tableView,因此实现此功能。</span>

主要需要一个类:

/* DragDropTableView */

/* DragDropTableView */

#import <Cocoa/Cocoa.h>

@interface DragDropTableView : NSTableView
{
}
@end

#import "DragDropTableView.h"

@implementation DragDropTableView
- (void) awakeFromNib
{
	// Register to accept filename drag/drop
	[self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
    
    [self setTarget:self];
    [self setDoubleAction:@selector(double_clicked)];

}

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
	// Need the delegate hooked up to accept the dragged item(s) into the model
	if ([self delegate]==nil)
	{
		return NSDragOperationNone;
	}
	
	if ([[[sender draggingPasteboard] types] containsObject:NSFilenamesPboardType])
	{
		return NSDragOperationCopy;
	}
	
	return NSDragOperationNone;
}

// Work around a bug from 10.2 onwards
- (unsigned int)draggingSourceOperationMaskForLocal:(BOOL)isLocal
{
	return NSDragOperationEvery;
}

// Stop the NSTableView implementation getting in the way
- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
{
	return [self draggingEntered:sender];
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
	int i;
	NSPasteboard *pboard;
	pboard = [sender draggingPasteboard];
	if ([[pboard types] containsObject:NSFilenamesPboardType])
	{
		id delegate = [self delegate];
		NSArray *filenames = [pboard propertyListForType:NSFilenamesPboardType];
		if ([delegate respondsToSelector:@selector(acceptFilenameDrag:)])
		{
			for (i=0;i<[filenames count];i++)
			{
				[delegate performSelector:@selector(acceptFilenameDrag:) withObject:[filenames objectAtIndex:i]];
			}
		}
		return YES;
	}
	return NO;
}	

- (void)double_clicked
{
    NSInteger rowNumber = [self clickedRow];
    NSLog(@"double clicked %ld\n", rowNumber);
    if (rowNumber > -1) {
        [self.delegate performSelector:@selector(tableView_doubleClicked:) withObject:(id)rowNumber];
    }
    
}

- (void)drawRow:(NSInteger)row clipRect:(NSRect)clipRect
{
    NSColor *myColor = [NSColor colorWithCalibratedRed:0 green:0 blue:0 alpha:1.0f];
    
    NSColor *color = (row % 2) ? myColor : [NSColor blackColor];
    [color setFill];
    NSRectFill([self rectOfRow:row]);
    [super drawRow:row clipRect:clipRect];
}

@end

在主Controller中,实现以下函数:

- (void) acceptFilenameDrag:(NSString *) filename; // also  a delegate function,

{

   NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:[self getFileName:filename], @"name",
                                 [mp3_info getTimeLong:filename], @"time",
                                  filename, @"pathname",
                                 nil];
    
    [arrayController_filenames addObject:dict];
    [fileInfoArray addObject:dict];
    
    [self savemp3List];
    [self resetRecentDocumentsLimit:500];
    
    // use recent Open file to cheat sandbox
    
    NSDocumentController* aa = [NSDocumentController sharedDocumentController];
    NSURL* aUrl = [NSURL fileURLWithPath:filename];
    [aa noteNewRecentDocumentURL:aUrl];


}

剩下的操作可以根据获得的文件自己操作即可。


Mac下默认的app的recent files是10或者user自己设置的。如果播放歌词会很容易突破这个限制。因此。需要改变一些环境设置。


- (void) resetRecentDocumentsLimit:(int)num
{
    NSTask *task;
    task = [[NSTask alloc] init];
    [task setLaunchPath: @"/usr/bin/defaults"];
    
    NSArray *arguments;
    
    arguments = [NSArray arrayWithObjects: @"write", @"info.popoping.MarmotPlayer", @"NSRecentDocumentsLimit", [NSString stringWithFormat:@"%d", num], nil];
    [task setArguments: arguments];
    
    NSPipe *pipe;
    pipe = [NSPipe pipe];
    [task setStandardOutput: pipe];
    
    NSFileHandle *file;
    file = [pipe fileHandleForReading];
    
    [task launch];
    
    NSData *data;
    data = [file readDataToEndOfFile];
    
    NSString *string;
    string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
    NSLog (@"returned:\n%@", string);
    
    [string release];
    [task release];
}

其中info.popoping.MarmotPlayer是自己的iden,可以换成自己的应用。

我得app已经下架。有需要mp3播放器的朋友可以联系我。邮件:reichtiger@163.com


附上抓图:


上图是绕过最近打开文件数的限制的。因此随便下了好多歌曲,拖进去。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值