views:

37

answers:

2

Hi all,

Generally, table has a fixed row height but according to my requirements I need to set height of each row according to content within it.

Can anyone suggest me some solution for it?

Thanks,

Miraaj

+1  A: 

The table view delegate protocol has a tableView:heightOfRow that lets you set the height of each row in the table view.

dj2
thanx for your quick reply... can you suggest me if there is some standard way to calculate height of row based on its content or I have to manipulate some ratio: based on characters in the string and width of row..
Miraaj
The one time I was doing it I was listening to the NSViewFrameDidChangeNotification notification and, when my specific view came up, I grabbed it's frame size. I'd store the size for the row cell and use the noteHeightOfRowsWithIndexesChanged method on the table to tell the table to update based on that row height changing.Probably not the best way to do it, but I only had to watch for a single webkit view that I was moving around.You can see the code, using MacRuby instead of Obj-C, at http://github.com/dj2/Rife/blob/master/lib/application.rb
dj2
Miraaj: You can ask each column for its width, and you can ask a string how much space it'd take up if drawn into a rectangle of a given size (e.g., one with the column's width and very large height). http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSString_AppKitAdditions/Reference/Reference.html#//apple_ref/occ/instm/NSString/boundingRectWithSize:options:attributes: For an image, you will have to do the math yourself, using the aspect ratio of the image's size.
Peter Hosey
A: 

Thanks all for your suggestions and help. This problem is now resolved using following code in tableView:heightOfRow -

float colWidth = [[[tableView tableColumns] objectAtIndex:1]width];

        NSString *content = [[[tempArray objectAtIndex:row] objectForKey:@"tValue"] string];

        float textWidth = [content sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"Lucida Grande" size:15],NSFontAttributeName ,nil]].width;

        float newHeight = ceil(textWidth/colWidth);

        newHeight = (newHeight * 17) + 13;
        if(newHeight < 47){
            return 47;
        }   
        return newHeight;
Miraaj