objective-c – 在NSTableView中实现拖放

objective-c – 在NSTableView中实现拖放,第1张

概述任何人都可以帮我在NSTableView中实现拖放 *** 作吗?我在下面使用了这段代码,但这些方法在执行期间没有被调用. - (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard{ // Copy the row num @H_404_0@ @H_404_0@ 任何人都可以帮我在NStableVIEw中实现拖放 *** 作吗?我在下面使用了这段代码,但这些方法在执行期间没有被调用.

- (BOol)tableVIEw:(NStableVIEw *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes topasteboard:(NSPasteboard*)pboard{    // copy the row numbers to the pasteboard.    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];    [pboard declareTypes:[NSArray arrayWithObject:@".gif"] owner:self];    [pboard setData:data forType:@".gif"];    return YES;}- (NSDragOperation)tableVIEw:(NStableVIEw*)tv valIDateDrop:(ID <NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NStableVIEwDropOperation)op{    // Add code here to valIDate the drop    if (row > [ m_imageArray count])        return NSDragOperationNone;    if (nil == [info draggingSource]) // From other application    {        return NSDragOperationNone;    }    else if (self == [info draggingSource]) // From self    {        return NSDragOperationNone;    }    else // From other documents     {        [tv setDropRow: row dropOperation: NStableVIEwDropAbove];        return NSDragOperationcopy;    }    NSLog(@"valIDate Drop");    return NSDragOperationcopy;}- (BOol)tableVIEw:(NStableVIEw *)atableVIEw acceptDrop:(ID <NSDraggingInfo>)info              row:(NSInteger)row dropOperation:(NStableVIEwDropOperation)operation{    NSPasteboard* pboard = [info draggingPasteboard];    NSData* rowData = [pboard dataForType:@".gif"];    NSIndexSet* rowIndexes = [NSKeyedUnarchiver unarchiveObjectWithData:rowData];    NSInteger dragRow = [rowIndexes firstIndex];    // Move the specifIEd row to its new location...}
解决方法 这是一个例子

#import "tableVIEwController.h"#import "Person.h"#define MyDataType @"MyDataType"@implementation tableVIEwController {    NSMutableArray *List;    NSInteger sourceIndex;}@synthesize tableVIEw;-(voID)awakeFromNib {    [tableVIEw registerForDraggedTypes:[NSArray arrayWithObjects:MyDataType,nil]];    List = [[NSMutableArray alloc] init];    Person *person = [[Person alloc] initWithname:@"Newton" age:64];    [List addobject:person];    person = [[Person alloc] initWithname:@"Archimedes" age:74];    [List addobject:person];    person = [[Person alloc] initWithname:@"Euler" age:44];    [List addobject:person];    person = [[Person alloc] initWithname:@"Poincare" age:24];    [List addobject:person];    person = [[Person alloc] initWithname:@"Gauss" age:34];    [List addobject:person];}-(voID)reArrange:(NSMutableArray *)array sourceNum:(NSInteger)sourceNum destNum:(NSInteger)destNum {    Person *person = List[sourceNum];    [List insertObject:person atIndex:destNum];    if (sourceNum < destNum) {        [List removeObjectAtIndex:sourceNum];    } else {        [List removeObjectAtIndex:sourceNum+1];    }    [tableVIEw reloadData];}#pragma mark - table// how many rows are there in the table?-(NSInteger)numberOfRowsIntableVIEw:(NStableVIEw *)tv {    return List.count;}// What object should I show in a particular cell?-(ID)tableVIEw:(NStableVIEw *)tv objectValueFortableColumn:(NStableColumn *)tableColumn row:(NSInteger)row {    Person *person = List[row];    Nsstring *IDentifIEr = [tableColumn IDentifIEr];    return [person valueForKey:IDentifIEr];}// Should I accept the drag with the rows specifIEd by rowIndexes? If YES then place the data on the provIDed paste board.- (BOol)tableVIEw:(NStableVIEw *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes topasteboard:(NSPasteboard *)pboard {    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];    [pboard declareTypes:[NSArray arrayWithObject:MyDataType] owner:self];    [pboard setData:data forType:MyDataType];    sourceIndex = [rowIndexes firstIndex];    return YES;}// What kind of drag operation should I perform?- (NSDragOperation)tableVIEw:(NStableVIEw*)tv valIDateDrop:(ID )info proposedRow:(NSInteger)row proposedDropOperation:(NStableVIEwDropOperation)op {    return op == NStableVIEwDropAbove; // SpecifIEs that the drop should occur above the specifIEd row.}// The mouse button was released over a row in the table vIEw,should I accept the drop?- (BOol)tableVIEw:(NStableVIEw*)tv acceptDrop:(ID )info row:(NSInteger)row dropOperation:(NStableVIEwDropOperation)op {    [self reArrange:List sourceNum:sourceIndex destNum:row]; // let the source array reflect the change    return YES;}@end
@H_404_0@ 总结

以上是内存溢出为你收集整理的objective-c – 在NSTableView中实现拖放全部内容,希望文章能够帮你解决objective-c – 在NSTableView中实现拖放所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1008293.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存