Greets!
I have a View-based NSTableView with Custom View as a cell (NSTableCellView class). There is NSTextField inside custom cell.
I need to display 3 lines of text inside NSTextField and after clicking on cell content must grow up to fit full text. So, I need to increment height of NSTextField and height of NSCTableCellView by click in other words.
Alghoritm is:
- Handle click on NSTableCellView
- Calculate new sizes of NSTextField and NSTableCellView
- SetFrameSize to NSTextField through IBOutlet
- Store new NSTableCellView height value in class and invoke function "RowHeightChanged" through Notification Center
- Set new row height from class value
This is a code snippet: (i erased some unusefull things)
TickCellTableView:
@implementation TickCellTableView
// Other Constants
@synthesize textFieldInitHeight = _textFieldInitHeight;
@synthesize rowHeight = _rowHeight;
@synthesize rowIndex = _rowIndex;
- (void)mouseDown:(NSEvent *)theEvent {
if (![self.superview isKindOfClass:[NSTableCellView class]])
[super mouseDown:theEvent];
CGFloat TextFieldFitHeight = [self getHeightForStringDrawing: self.textField.stringValue
withFont:[self.textField font]
forTextWidth:[self.textField frame].size.width + 10.0];
if ([self.textField frame].size.height < TextFieldFitHeight)
{
NSSize size = [self.textField frame].size;
size.height = TextFieldFitHeight;
[self.textField setFrameSize:size];
self.rowHeight = [self frame].size.height + (TextFieldFitHeight - _textFieldInitHeight);
[[NSNotificationCenter defaultCenter] postNotificationName:@"RowHeightChanged"
object:self.rowIndex];
}
}
@end
NSTableView Delegate:
@implementation DisplayTicksView
- (void)awakeFromNib
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(RowHeightChanged:)
name: @"RowHeightChanged"
object:nil];
}
-(void)RowHeightChanged:(NSNotification *)notification
{
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:[notification.object integerValue]];
[tableView noteHeightOfRowsWithIndexesChanged:indexSet];
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
return [ticksArray count];
}
@end
The problem is when i clicked on the NSTextField it's frame grow up just for the second and return to init size, after that starts animation to set property height of cell. In result - Cell height is up, but NSTextField none. I tried to disable animation - no result. How can i fix that?
Appologise my English :)