6

tableView がドラッグ ソースの場合、NSTableView または NSTableCellView をサブクラス化してカスタム ドラッグ イメージを作成する必要がありますか?

そうでない場合、これを行うために欠けている魔法の方法は何ですか? しっかりしたものが見つからないようです。

NSTableCellView のサブクラスには、(少し不思議なことに) オーバーライドすることができます:

@property(retain, readonly) NSArray *draggingImageComponents(一緒に合成されるNSDraggingImageComponentインスタンス の配列 (どのような方法で合成されるかは誰にもわかりません...))

NSTableView自体には

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

しかし、これらは確かにサブクラス化オプションのようです。

ここで別のテクニックを知っている人はいますか?(10.8以上が対象)

4

2 に答える 2

4

まるでNSTableViewCell

@property(retain, readonly) NSArray *draggingImageComponents

は自動的には呼び出されませんが、ビュー ベースのテーブル ビューでは明示的に参照する必要があります。-draggingImageComponentsオーバーライドして、ドラッグ イメージの合成に使用されるコンポーネントを提供できます。

- (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;};
         }];
    }
}
于 2015-05-13T11:30:12.207 に答える
2

データソースでは、たとえばtableView:draggingSession:willBeginAtPoint:forRowIndexes:メソッドから画像を設定する必要があります

   [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];
    }];
于 2014-03-02T12:48:54.113 に答える