NSCollectionView subclass doesn't call drawRect during drag session despite setNeedsDisplay

Posted by Alain Vitry on Stack Overflow See other posts from Stack Overflow or by Alain Vitry
Published on 2010-04-08T21:35:05Z Indexed on 2010/04/09 1:23 UTC
Read the original article Hit count: 611

Greetings,

I am puzzled as to how and when drawRect is supposed to be called in a NSCollectionView subclass.

I implement drag and drop operation in order to move NSCollectionViewItems within the collection, and would like to draw a visual indication of where the drop would end.

The subclass does not call drawRect during the drag session. (It does during scroll)

Is this the intended operation ? Any hint on how to implement this behavior properly are welcome.

A full xcode project of the following code is available at: http://babouk.ovh.org/dload/MyCollectionView.zip

Best regards

Code sample:

@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)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

© Stack Overflow or respective owner

Related posts about cocoa

Related posts about nscollectionview