【问题标题】:Custom drag image with NSTableView as drag source以 NSTableView 作为拖动源的自定义拖动图像
【发布时间】:2013-07-09 07:06:09
【问题描述】:

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+是目标)

【问题讨论】:

  • 在进一步的测试中,首先从 NSTableCellView 开始(阻力最小的路径!)似乎虽然它们提供了一个图像,但它首先通过表格视图,表格视图在拖动图像时会有些奇怪在其剪辑视图上...开始拖动时剪辑视图中不可见的任何内容都不是拖动图像的一部分。

标签: drag-and-drop nstableview nstablecellview


【解决方案1】:

看起来好像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;};
         }];
    }
}

【讨论】:

    【解决方案2】:

    在您的data source 中,您需要设置图像,例如从您的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];
        }];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多