Custom drag image with NSTableView as drag source

做~自己de王妃 提交于 2019-12-18 08:24:45

问题


Is it necessary to subclass NSTableView or NSTableCellView to create a custom drag image when the tableView is the drag source?

If not, what is the magic method I am missing to do this? I cannot seem to find anything solid.

NSTableCellView subclasses have can (slightly mysteriously) override:

@property(retain, readonly) NSArray *draggingImageComponents (array of NSDraggingImageComponent instances that will get composited together (who knows in what fashion they get composited...))

NSTableView itself has

- (NSImage *)dragImageForRowsWithIndexes:(NSIndexSet *)dragRows tableColumns:(NSArray *)tableColumns event:(NSEvent *)dragEvent offset:(NSPointPointer)dragImageOffset

But these are indeed subclassing options it seems.

Anybody know another technique here? (10.8+ is target )


回答1:


It looks as if NSTableViewCell

@property(retain, readonly) NSArray *draggingImageComponents

does not get called automatically but must be explicitly referenced for view based table views. -draggingImageComponents can be overridden to supply the components used to composite the drag image.

- (void)tableView:(NSTableView *)tableView draggingSession:(NSDraggingSession *)session willBeginAtPoint:(NSPoint)screenPoint forRowIndexes:(NSIndexSet *)rowIndexes
{
    // configure the drag image
    // we are only dragging one item - changes will be required to handle multiple drag items
    BPPopupButtonTableCellView *cellView = [self.columnsTableView viewAtColumn:0 row:rowIndexes.firstIndex makeIfNecessary:NO];
    if (cellView) {

        [session enumerateDraggingItemsWithOptions:NSDraggingItemEnumerationConcurrent
                                           forView:tableView
                                           classes:[NSArray arrayWithObject:[NSPasteboardItem class]]
                                     searchOptions:nil
                                        usingBlock:^(NSDraggingItem *draggingItem, NSInteger idx, BOOL *stop)
         {
             // we can set the drag image directly
             //[draggingItem setDraggingFrame:NSMakeRect(0, 0, myWidth, myHeight) contents:myFunkyDragImage];

             // the tableview will grab the entire table cell view bounds as its image by default.
             // we can override NSTableCellView -draggingImageComponents
             // which defaults to only including the image and text fields
             draggingItem.imageComponentsProvider = ^NSArray*(void) { return cellView.draggingImageComponents;};
         }];
    }
}



回答2:


In your data source you'll want to set the images e.g. from your tableView:draggingSession:willBeginAtPoint:forRowIndexes: method like so

   [session enumerateDraggingItemsWithOptions:NSDraggingItemEnumerationConcurrent
                                      forView:tableView
                                      classes:[NSArray arrayWithObject:[NSPasteboardItem class]]
                                searchOptions:nil
                                   usingBlock:^(NSDraggingItem *draggingItem, NSInteger index, BOOL *stop)
    {
       [draggingItem setDraggingFrame:NSMakeRect(0, 0, myWidth, myHeight)
                             contents:myFunkyDragImage];
    }];


来源:https://stackoverflow.com/questions/17586110/custom-drag-image-with-nstableview-as-drag-source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!