How to support multi-line text cells in a tableview

Discussion in 'Mac Programming' started by tmeehan, Mar 10, 2009.

  1. tmeehan macrumors newbie

    Feb 25, 2009
    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.

  2. kainjow Moderator emeritus


    Jun 15, 2000
    You probably need to subclass NSTextFieldCell and mess with the editing frame methods. editWithFrame:inView:editor:delegate:event: might be what you need.
  3. tmeehan thread starter macrumors newbie

    Feb 25, 2009
    May have solved it myself, but thanks for you suggestion.:)

    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.

Share This Page