Error when changing tableView height

I am getting strange behavior when I change a tableview size after creation. I create the TV with 10 rows, and a height of 200, which is enough to show 5 rows. This leads to this Before screenshot:

So far so good. After 5 seconds, I double the height of the TV, so all 10 rows will show. Instead, I get this After screenshot:

As you can see, it does not actually double in height, it only increases a bit. Furthermore, it actually moves downward on the screen, as if the y had changed. However, when I print() these values I get:

y:0 height: 200 contentHeight: 200 BEFORE

y:0 height: 400 contentHeight: 400 AFTER

So the y has not changed at all in the code. And the height has doubled, exactly like it should have. But that is not what shows on screen.

Can anyone explain this? Is changing a TV height not supported?

Here is the code I used to generate the screenshots.

[lua]local widget = require(‘widget’)

– Handle row rendering
local function onRowRender( event )
    – Get reference to the row group
    local row = event.row

    – Cache the row “contentWidth” and “contentHeight” because the row bounds can change as children objects are added
    local rowHeight = row.contentHeight
    local rowWidth = row.contentWidth

    local rowTitle = display.newText( row, "Row " … row.index, 0, 0, nil, 14 )
    rowTitle:setFillColor( { 1, 0, 0 } )

    – Align the label left and vertically centered
    rowTitle.anchorX = 0
    rowTitle.x = 0
    rowTitle.y = rowHeight * 0.5
end

– Create the tableView
local tableView = widget.newTableView
{
    x = display.contentWidth/2,
    y = 0,
    height = 200,
    width = 320,
    onRowRender = onRowRender
}
tableView.anchorY = 0
print(tableView.y, tableView.height, tableView.contentHeight)

– Insert rows
for i = 1, 10 do
    tableView:insertRow{}
end

local function resizeTV()
    tableView.height = 2*tableView.height
    print(tableView.y, tableView.height, tableView.contentHeight)
end

– 5 seconds after creation, increase the table view height
timer.performWithDelay(5000, resizeTV)[/lua]

Generally speaking, I don’t think we intended to resize tableViews that way. Probably the best thing is to remove all the rows, resize it and re-insert the rows. It seems harsh, but it’s a quick action unless the tableView is huge.

Rob

Thanks Rob! When you say “it’s a quick action unless the tableView is huge”, how many rows is huge? A thousand? A million?

I would say trying to insert more than 100 rows might take more than an update frame cycle. But even if you find 100 too slow, you can insert enough to redraw the tableView let the screen update happen then add more records.

Rob

Unfortunately, deleting the table rows with deleteAllRows() did not resolve the issue. Destroying the TV and recreating it did fix it (obviously), but that seems drastic. Is there an easier solution?

Good point, if you need to change the tableView size removing and recreating the tv is needed. If you’re changing row sizes, removing and re-inserting takes care of that.

Harsh, yes, but it’s the way that works.s

Rob

Generally speaking, I don’t think we intended to resize tableViews that way. Probably the best thing is to remove all the rows, resize it and re-insert the rows. It seems harsh, but it’s a quick action unless the tableView is huge.

Rob

Thanks Rob! When you say “it’s a quick action unless the tableView is huge”, how many rows is huge? A thousand? A million?

I would say trying to insert more than 100 rows might take more than an update frame cycle. But even if you find 100 too slow, you can insert enough to redraw the tableView let the screen update happen then add more records.

Rob

Unfortunately, deleting the table rows with deleteAllRows() did not resolve the issue. Destroying the TV and recreating it did fix it (obviously), but that seems drastic. Is there an easier solution?

Good point, if you need to change the tableView size removing and recreating the tv is needed. If you’re changing row sizes, removing and re-inserting takes care of that.

Harsh, yes, but it’s the way that works.s

Rob