PDA

View Full Version : How to support multi-line text cells in a tableview




tmeehan
Mar 10, 2009, 09:03 AM
Does anyone know how to support multi-line text fields in a TableView? I can see how to do it with a NSTextField. In that case you can use the delegate methods to control the behavior. But the NSTableColumn implements NSTextFieldCell not NSTextField. You would think one would inherite from the other but it is not the case.

I really need to be able to enter more than one line of text (not text wrapping) delimited with carriage returns. Any help with this would be greatly appreciated.

Thanks.



kainjow
Mar 10, 2009, 09:34 AM
You probably need to subclass NSTextFieldCell and mess with the editing frame methods. editWithFrame:inView:editor:delegate:event: might be what you need.

tmeehan
Mar 10, 2009, 03:50 PM
For those who may struggle with this, here is what I did.

When I discovered what the window's field editor is all about I was convinced that is what I needed to use. I guess each window has a default field editor which is a TextView that sits around and handles the text editing functions for all of the text related items in the window. Sort of an invisisble TextView that does the heavy lifting. It's behavior can be modified via it's delegate methods.

1) Subclass textView and override the textView:doCommandBySelector: method as such:

- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector {
BOOL retval = NO;
if (commandSelector == @selector(insertNewline:)) {
retval = YES;
[fieldEditor insertNewlineIgnoringFieldEditor:nil];
}
return retval;
}

2) Override the window delegate's windowWillReturnFieldEditor:toObject: method. Here's my code:

-(id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject
{
if ([anObject isKindOfClass:[ListBoxControl class]])
{
return [[[myTextView alloc] init] autorelease];
}
return nil;
}

In my case the only class instance that I wanted to modify the behavior for was an instance of class ListBoxControl. If the object requesting text editing services is any other than of class type ListBoxControl, then my custom field editor is not used and the default field editor is used.