1

我正在使用NSButtonCell子类在同一个单元格中显示复选框、图像和标签。现在我想通过双击或输入键在大纲视图中编辑标签。
NSButtonCell子类:

- (void)dealloc {
    [image release];
    image = nil;
    [super dealloc];
}

-(id)copyWithZone:(NSZone *)zone {
    ImageAndTextAndCheckBox *cell = (ImageAndTextAndCheckBox *)[super copyWithZone:zone];
    cell->image = [image retain];
    return cell;
}

- (void)setImage:(NSImage *)anImage {
    if (anImage != image) {
        [image release];
        image = [anImage retain];
        [image setSize:NSMakeSize(kIconImageSize, kIconImageSize)];
    }
}

- (NSImage *)image {
    return image;
}


#define KNImageAndTextButtonCellPadding 5 // Distance between the end of the image and the start of the text.

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
    // Draw the original button first, then the image.
    //shift check box to right
    NSRect newCellFrame = cellFrame;
    newCellFrame.origin.x =  newCellFrame.origin.x + 5;
    cellFrame = newCellFrame;

    [super drawWithFrame:cellFrame inView:controlView];

    if (image != nil) {

        NSRect imageFrame;

        NSSize imageSize = [image size];
        NSDivideRect(cellFrame, &imageFrame, &cellFrame, KNImageAndTextButtonCellPadding + imageSize.width, NSMinXEdge);

        imageFrame.origin.x += imageFrame.size.width;
        imageFrame.size = imageSize;

        if ([controlView isFlipped]) {
            imageFrame.origin.y += ceil((cellFrame.size.height + imageFrame.size.height) / 2);
        } else {
            imageFrame.origin.y += ceil((cellFrame.size.height - imageFrame.size.height) / 2);
        }

        BOOL oldFlipped = [image isFlipped];
        [image setFlipped:![controlView isFlipped]];
        [image compositeToPoint:imageFrame.origin operation:NSCompositeSourceOver];
        [image setFlipped:oldFlipped];

    }   
}


-(BOOL)trackMouse:(NSEvent *)theEvent inRect:(NSRect)cellFrame ofView:(NSView *)controlView untilMouseUp:(BOOL)untilMouseUp {

    // Make sure that the mouse only gets tracked if it is clicked on the check box, otherwise 
    // the row will be checked/unchecked whenever the user clicks on any part of the cell (which is bad).
    if ([theEvent clickCount] > 1)
    {
        NSLog(@"do double-click action" );
    }
    else
    {
         NSLog(@"do single-click action" );
    }
    NSPoint event_location = [theEvent locationInWindow];
    NSPoint localPoint = [controlView convertPoint:event_location fromView:nil];
    //shift check box mouse event to right
    NSRect newCellFrame = cellFrame;
    newCellFrame.origin.x =  newCellFrame.origin.x + 5;
    cellFrame = newCellFrame;
    if (localPoint.x <= cellFrame.origin.x + 16) {
        return [super trackMouse:theEvent inRect:cellFrame ofView:controlView untilMouseUp:untilMouseUp];
    } else {
        return NO;
    }
}

-(NSRect)drawTitle:(NSAttributedString *)title withFrame:(NSRect)frame inView:(NSView *)controlView {

    // Adjust the rect so we don't interfere with the image's location

    if (image != nil) {
        //NSLog(@"orig %f %f %f %f", frame.size.width,frame.size.height,frame.origin.x,frame.origin.y);
        NSRect newFrame = NSOffsetRect(frame, [[self image] size].width + KNImageAndTextButtonCellPadding, 0);
        newFrame.size.width -= ([[self image] size].width + KNImageAndTextButtonCellPadding);
        //NSLog(@"drawTitle %f %f %f %f", newFrame.size.width,newFrame.size.height,newFrame.origin.x,newFrame.origin.y);
        return [super drawTitle:title withFrame:newFrame inView:controlView];

    } else {
        return [super drawTitle:title withFrame:frame inView:controlView];
    }

}
- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent {
    NSLog(@"%@", anObject);


//  id node = [anObject itemAtRow: [anObject selectedRow]];
//  int length = [[node relativePath] length];
    NSRect newFrame = NSOffsetRect(aRect,  KNImageAndTextButtonCellPadding+kIconImageSize+ 16, 0);
    newFrame.size.width -= (KNImageAndTextButtonCellPadding+kIconImageSize+ 16);
    [super editWithFrame:newFrame inView:controlView editor:textObj delegate:anObject event:theEvent];
}  

我总是得到 1 (布尔值)

- (void)outlineView:(NSOutlineView *)ov     
     setObjectValue:(id)obj 
     forTableColumn:(NSTableColumn *)tableColumn 
             byItem:(id)item  

NSOutlineView委托方法。(因为 NSButtonCell)但我想编辑标签。

  1. 如何编辑标签?
  2. 如何处理双击?
  3. 在按下回车键之前:
    在此处输入图像描述

之后
在此处输入图像描述 如何更改颜色文本?它显示白色。

它应该像 在此处输入图像描述(带复选框)

谁能帮帮我?

4

0 回答 0