Inserting an image & text in NSTableView

Discussion in 'Mac Programming' started by dharma, Feb 20, 2006.

  1. macrumors newbie

    Joined:
    Feb 20, 2006
    #1
    Hi Folks,

    I'd like to insert image and text in the same cell in NSTableview, and the images are different in the different rows. What I know is to use a delegate method

    -(void)tableView: WillDisplayCell: forTableColumn: row:
    {

    }

    But I've no idea how I should implement the method. Would anyone help me please. I'm really new in Cocoa, so your detailed explanation is very helpful to me.

    Thanks
     
  2. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #2
    From borkware http://www.borkware.com/quickies/one?topic=NSString
    I'd probably cache the attributed string somewhere so you don't have to generate everytime the tableview gets updated
     
  3. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #3
    Is that enough detail or do you need help on how to actually write the datasource/delegate methods? In this case I don't think you'd need to use the willDisplayCell method as I think you're OK just returning the NSAttributedString in the objectValueForTableColumn: row: datasource method. I've not actually done this though so I might be wrong. I have used willDisplayCell before for more complicated things with custom cells but as I said I don't think you need that here.
     
  4. thread starter macrumors newbie

    Joined:
    Feb 20, 2006
    #4
    Thanks caveman_uk for your help.:) It works, but the text sits at the bottom of the icon's height. It'll be perfect if the text is at the center height.
     
  5. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #5
    Ahhh, your icon is a lot bigger than the text size then. Do you want it that big? If not, resize it using NSImage's -setSize. Otherwise you'll need to create a custom NSCell class and handle all the drawing yourself. There's plenty of gotchas that await there :rolleyes: including the 'Thou must implement copyWithZone: properly' gotcha.

    You could always cheat and actually show the icon and text in different table columns - just hide the column borders.
     
  6. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #6
    Incidently, I've just noticed the code I posted earlier has a memory leak - attrname needs to be released before the return.
     
  7. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    I think there might be another way. Can you not alter the baseline for the text part of the attributed string to move it up a bit?
     

Share This Page