GridView addRow height problem

I am trying to add rows to GridView and not able to get expected row height correctly.

override func viewDidLoad() {
        super.viewDidLoad()

        // Remove the row in IB designer
        gridView.removeRow(at: 0)

        let image = NSImage(named: NSImage.colorPanelName)!
        //image.size = NSMakeSize(80, 80)
        let imageView = NSImageView(image: image)
        imageView.imageFrameStyle = .grayBezel
        imageView.imageScaling = .scaleAxesIndependently
        imageView.frame = NSMakeRect(0, 0, 80, 80)
        let label = NSTextField(labelWithString: "test text")
        let row = gridView.addRow(with: [imageView, label])
        row.height = 80
    }

What was wrong with my code?

Replies

How is gridView defined ? In storyboard ?

Have you specified its attributes and size ?

  • I don't want to define it in IB. I want to dynamically add rows and define width and height.

Add a Comment

I found out why, but it's not expected:

imageView.frame = NSMakeRect(0, 0, 80, 80)
let row = gridView.addRow(with: [imageView, label])
print(gridView.column(at: 0).width, row.height)
// output
// 80.0 1.1754943508222875e-38

Why the initial height is 1.175 for a newly added row (note I already resized image view to 80x80)?

Even later on I explicitly set row.height = 80, but it does not work; the image view is squeezed.

In IB designer, I am not even able to resize an added image view in first cell. The width and height is fixed at 32; if I try to modify the value, it automatically reverts back to 32.

It seems GridView is performing some kind of invisible magic, which I don't have any knowledge of.

So, how do you define the grid in code ? Do you set its frame ?

Once you set row.height = 80, do you call for gridView.needsUpdate ?

Finally, I solved my own question. It seems I have to set yPlacement to .fill.