当前位置: 动力学知识库 > 问答 > 编程问答 >

objective c - Resizing the NSTableView programmatically

问题描述:

How to implement the feature of resizing NSTableView created programmatically ? Interface builder should not be used. It should be like click and drag to change the size of the NSTableView. Is it possible? If yes, please help. . . .

网友答案:

I am afraid you need to write a bit of code to make it working. This is how I would do it.

Make a special Resize View that will track the mouse events and call delegate methods providing how the tracking position changes.

- (void)mouseDown:(NSEvent *)theEvent
{
    _startPoint = [theEvent locationInWindow];
    [_delegate resizingDidStart];
}

- (void)mouseDragged:(NSEvent *)theEvent
{
    NSPoint hitPoint = [theEvent locationInWindow];
    [_delegate resizeWithDeltaX:(hitPoint.x - _startPoint.x) deltaY:(hitPoint.y - _startPoint.y)];
}

Put this view in the right bottom corner of the Base View. Set the autoresizing mask so this view always stays in the right bottom corner.

Put the table view along with its scroll view on to the Base View. Set autoresizing mask of the scroll view so its size and width are sizeable.

In the delegate of the Resize View process changes in the mouse position and set the frame of the Base View.

- (void)resizingDidStart
{
    _initialRect = [_baseView frame];
}

- (void)resizeWithDeltaX:(CGFloat)deltaX deltaY:(CGFloat)deltaY
{
    [_baseView setFrame:NSMakeRect(_initialRect.origin.x, _initialRect.origin.y + deltaY, _initialRect.size.width + deltaX, _initialRect.size.height - deltaY)];
}

Of course the scroll view should be under the resize view. You can draw some ind of a handle on the resize view, etc.

分享给朋友:
您可能感兴趣的文章:
随机阅读: