Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]

When should I create a new object inside an "onRender" for a TableView?
Started by sieler2 Apr 09 2018 02:49 PM

1 reply to this topic
This topic has been archived. This means that you cannot reply to this topic.
[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Contributor

  • 108 posts
  • Corona SDK



Exploring widget.newTableView, I notice that the row render function is frequently called even when the row's display object is already there.


I.e., my render code, below, seems to work fine.  When first called for a given row ("index" value),

I create a text object and then say "row.done = true".  When called again for the same row, 

I find that *almost* always row.done is still true, and the object still exists with the correct contents.


But, after a scroll, sometimes on_row_render is called for a row index I've rendered before and "row.done" is false/nil (and, in those cases, the 'address' of the row object is different than the prior time for the same index).


I presume that what's happening is that the TableView widget conditionally releases some of the

row objects based on some internal criteria (e.g., how many object does it have allocated, how

many does it want allocated, and was the object recently scrolled off the screen).


That raises the question: *is* it correct for me to rely (with a test) on the prior contents, or should

I assume that I have to re-render on every call?


I'm hoping the answer is: if row.done is valid, you can rely on that, otherwise re-render :)




local function on_row_render (event)
   local row_height, row_title, row, index

   row = event.row     -- Get reference to the row group

   index = row.index

      -- Cache the row "contentWidth" and "contentHeight" because the row bounds can change as children objects are added

   local row_height = row.contentHeight

   if not row.done then
      row_title = display.newText (row, "abc " .. row.index, 0, 0, nil, 14)
      row_title:setFillColor (0)  -- color of the text (0 = black)
         -- Align the label left and vertically centered

      row_title.anchorX = 0
      row_title.x = 1    -- 0 works, but seems a tad close to the edge of the rectangle
      row_title.y = math.floor (row_height * 0.5)
      row.done = true
      row.title = row_title
      -- print ("new row.title = ", row.title)
      -- print ("old row.title = ", row.title)

   return true

end -- on_row_render proc

[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Corona Geek

  • 2,197 posts
  • Corona SDK

IMHO, Just rerender every time it is simple and ensures you will never get an unrendered row.