问题描述
当tableView为拖拽源时,是否需要继承NSTableView或NSTableCellView创建自定义拖拽图片?
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 子类可以(有点神秘地)覆盖:
NSTableCellView subclasses have can (slightly mysteriously) override:
@property(retain, readonly) NSArray *draggingImageComponents
(将组合在一起的 NSDraggingImageComponent 实例数组(谁知道它们以何种方式组合...)
@property(retain, readonly) NSArray *draggingImageComponents
(array of NSDraggingImageComponent instances that will get composited together (who knows in what fashion they get composited...))
NSTableView本身有
- (NSImage *)dragImageForRowsWithIndexes:(NSIndexSet *)dragRows tableColumns:(NSArray *)tableColumns event:(NSEvent *)dragEvent offset:(NSPointPointer)dragImageOffset
但这些看起来确实是子类化选项.
But these are indeed subclassing options it seems.
这里有人知道另一种技术吗?(10.8+是目标)
Anybody know another technique here?(10.8+ is target )
推荐答案
好像NSTableViewCell
@property(retain, readonly) NSArray *draggingImageComponents
不会自动调用,但必须为基于视图的表视图显式引用.-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;};
}];
}
}
这篇关于以 NSTableView 作为拖动源的自定义拖动图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!