尽管setNeedsDisplay,NSCollectionView子类在拖动会话期间不会调用drawRect

时间:2010-04-08 21:35:06

标签: objective-c cocoa drag-and-drop nscollectionview

问候,

我很困惑如何以及何时在 NSCollectionView 子类中调用 drawRect

我实施拖放操作,以便在集合中移动 NSCollectionViewItems ,并希望直观地显示丢弃结束的位置。

子类在拖动会话期间不调用drawRect。 (它在滚动期间)

这是预期的操作吗? 有关如何正确实现此行为的任何提示都是受欢迎的。

以下代码的完整xcode项目位于: http://babouk.ovh.org/dload/MyCollectionView.zip

祝你好运

更新 示例代码错误。对registerForDraggedTypes的调用使得集合按预期触发drawRect。

代码示例:

@interface CollectionViewAppDelegate : NSObject <NSApplicationDelegate> 
{
    NSWindow            *window;
    NSMutableArray      *collectionContent;
}
/* properties declaration */
/* KVC compliance declarations */
@end

@interface MyCollectionView : NSCollectionView
@end

@interface ItemModel
{
    NSString *name;
}
@property (copy)  NSString *name;
@end

@implementation MyCollectionView
- (void)awakeFromNib {
[self registerForDraggedTypes:[NSArray arrayWithObjects:NSStringPboardType, nil]];
}

- (void)drawRect:(NSRect)rect {
    NSLog(@"DrawRect");
}

- (void)mouseDragged:(NSEvent *)aEvent {
    NSPoint     localPoint = [self convertPoint:[aEvent locationInWindow] fromView:nil];
    [self dragImage:[NSImage imageNamed:@"Move.png"] 
                 at:localPoint
             offset:NSZeroSize
              event:aEvent 
         pasteboard:nil 
             source:self 
          slideBack:NO];
}

- (BOOL)prepareForDragOperation:(id < NSDraggingInfo >)sender {
    return YES;
}

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
    return NSDragOperationEvery;
}

- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender {
    [self setNeedsDisplay:YES];
    return NSDragOperationEvery;
}

- (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal {    
    return NSDragOperationEvery;
}

- (void)mouseDown:(NSEvent *)theEvent {
}

@end

@implementation CollectionViewAppDelegate

@synthesize window, collectionContent, collectionView;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    NSMutableArray *data = [[NSMutableArray alloc] init];
  /* Fill in data */
    [self setCollectionContent:data];
    [data release];
}

/* dealloc */

/* KVC implementation */
@end

@implementation ItemModel
@synthesize name;
/* dealloc */
@end

1 个答案:

答案 0 :(得分:1)

我认为你不能成功地继承NSCollectionView的绘图例程。在内部,集合视图在核心动画层中布局,因此-drawRect:不用于呈现其内容。