when scrolling table view, the selected row was reset. How to keep selected status?

Hi Everyone,

Let me explain about the problem.

The row will be selected by Pressing or Tap at the row.

After row touch, if row.nameText.isselected is false,  color of bg (rect object) will be changed to blue color (selected row) but If row.nameText.isselected is true,  the color of bg (rect object) will be changed to write color (unselected row). It working fine.

but the problem is when I try to scroll table view down, some of selected row(blue color) will be changed to white color (unselect)  (it may be redraw the screen again, I don’t know about tableview behaviour).

This is my code below. Could you suggest me the solution for this ? How can I retain the selected row, when tableview is scrolled down or up. Thank you very much

local function onRowTouch( event )

    local phase = event.phase

    local row = event.target

    local params = event.target.params

    

if event.phase == ‘press’ or event.phase == ‘tap’ then

         row.nameText.isselected = not row.nameText.isselected

         if(row.nameText.isselected) then

             row.nameText:setFillColor( 1, 1, 1 )       

              row.bg:setFillColor( 0.0, 0.58, 0.90)      ----- Select Row Here

         else

              row.nameText:setFillColor(0, 139/255, 166/255)

            row.bg:setFillColor( 1, 1, 1)    ----- Unselect Row Here

    end

   

end

local function onRowRender( event )

    local phase = event.phase

    local row = event.row

  local params = event.row.params

    row.bg = display.newRect( 0, 0, display.contentWidth+40, 30 )

    row.bg.anchorX = 0

    row.bg.anchorY = 0

    row.bg:setFillColor(1,1,1)

    row:insert(row.bg)

if (row.params) then    

      row.nameText = display.newText(params.name, 0,0, fontNameRegular, 14 )

      row.nameText.anchorX = 0

      row.nameText.anchorY = 0.5

      row.nameText:setFillColor(0, 139/255, 166/255)

      row.nameText.y =  30/2 -1

      row.nameText.x = 30 + display.screenOriginX

      row.nameText.id = params.id

      row.nameText.isselected = params.isselected

      row:insert(row.nameText)

   end

end

Set a variable that stores the row id that is currently selected.

When it comes back on screen (via row render) check if the rows id is equal to the selected rows id and if so, recolor it.

That’s one way of doing it.

Hope this helps

Thank you very much. It’s working! :slight_smile:

Glad to hear it. Happy coding :slight_smile:

Set a variable that stores the row id that is currently selected.

When it comes back on screen (via row render) check if the rows id is equal to the selected rows id and if so, recolor it.

That’s one way of doing it.

Hope this helps

Thank you very much. It’s working! :slight_smile:

Glad to hear it. Happy coding :slight_smile: