views:

151

answers:

1

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

Update: The example code is wrong. A call to registerForDraggedTypes, makes the collection trigger drawRect as expected.

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)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
A: 

I don't think you can successfully subclass NSCollectionView's drawing routines. Internally the collection views are laid out in Core Animation Layers and so -drawRect: is not used to render their content.

Rob Keniger
Thank you for your answer, you are right.However, drawRect is called as expected in the example, after I fixed a missing call to registerForDraggedTypes.The problem is still there in my full size application though. I keep investigating.
Alain Vitry