I searched around but could not find an answer to the problem of enforcing a particular window size so that only "whole" rows are shown in a table view. I have archives of this list going back to 2002 but nothing suitable popped up.

Here's the code in objective-j (Cappuccino framework).

/*
 * Enforce a window size that shows only whole rows instead
 * of the default fractional rows.
 */
- (void)windowDidResize:(CPNotification)notification
{
    var window = [self window],
        superFrame = [window frame],
        tableSize = [scrollView contentSize],
        frameDelta = superFrame.size.height - tableSize.height,
        rowHeight = [theTable rowHeight],
        visibleRows = FLOOR(tableSize.height / rowHeight);

    tableHeight = rowHeight * visibleRows;

    superFrame.size.height = tableHeight + frameDelta;

    [window setFrame:superFrame];
}


Four items of note regarding this code.

1. This should be done in the windowWillResize:toSize delegate method instead of a direct call to setFrame but Cappuccino doesn't support that delegate yet.

2. The line "window = [self window]" should probably be "window = [notification object]" when translating to objective-c but for some reason that isn't working right in Cappuccino.

3. FLOOR is a macro corresponding to whatever is Math.floor in your host language. Adjust accordingly.

4. Computing a frame delta between the table view's visible content size and the window frame gives us the correct behavior even when there are other controls or elements inside the window. Some sample code I saw was tracking the size of specific fields and doing a lot more math just to accomplish this task.

If anyone has an improved methodology, please share.

cr

_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to