TableView highlight row selected

I’ve been integrating TableView in my app and I was wondering how to keep the selected row highlighted?

When you click on one, you see the row in blue but only for 1 second. Is there an easy way to do it? Am I missing a parameter somewhere?

Also when I click another row, I want the previous one to become white again.

Can somebody help me explaining how I can do it? 

Thank you very much!

Claude

you could always set the fillColor on the row to something (if it’s not a masked tableView) or just put a colored background on the row that’s selected.  Of course you would have to have a way to go through and set the backgrounds to your non-selected color in your code. 

There is not a built in way to do it.

In your row touch handler:

event.target:setRowColor(

    default = { r, g, b, a },

    over = { r, g, b, a }

)

There is built in support for this but i do not believe it is documented. This won’t work if the tableView is masked, as Rob already pointed out.

Ok thanks I will try that, it is masked so I guess I will loop over the rows and change the color for the highlighted one (with a custom flag) and reset the others on the event. 

For the looping codes, should I put that on the row touch event or or the row render one?

Thank you again

Try the setRowColor function in your row touch event. 

This my code on the onRowTouch event, now the row stays selected (good) but the loop doesn’t change the old one to white, any ideas?

local function onRowTouch( event )

    

   local phase = event.phase

    for i = 1, tableView:getNumRows() do

        tableView._view._rows[i]._rowColor.default = {255, 255, 255}

        tableView._view._rows[i]._rowColor.over = {255, 255, 255}

     end

   if ( “press” == phase ) then

          event.target:setRowColor{

           default = { 19, 144, 255 } ,

           over = { 19, 144, 255 }

         }

   end

end

Does this work? (untested code)

ocal function onRowTouch( event ) local phase = event.phase for i = 1, tableView:getNumRows() do tableView.\_view.\_rows[i]:setRowColor( { default = {255, 255, 255}, over = {255,255,255}} ) end if ( "press" == phase ) then event.target:setRowColor{ default = { 19, 144, 255 } , over = { 19, 144, 255 } } end end

I tried that and I have the following error:
 
attempt to call method ‘setRowColor’ (a nil value)

local function onRowTouch( event )     local phase = event.phase     local row = event.target           for i = 1, list:getNumRows() do         list.\_view.\_rows[i].\_rowColor.default = {255, 255, 255}         list.\_view.\_rows[i].\_rowColor.over = {math.random(255), math.random(255), math.random(255)}      end          if "press" == phase then     elseif "release" == phase then         -- operation     end end  

or on create row using this code:

list:insertRow{             height = 72,             rowColor = {                      default = { 255, 255, 255},                     over = { 255, 0, 0},             }, }   

you could always set the fillColor on the row to something (if it’s not a masked tableView) or just put a colored background on the row that’s selected.  Of course you would have to have a way to go through and set the backgrounds to your non-selected color in your code. 

There is not a built in way to do it.

In your row touch handler:

event.target:setRowColor(

    default = { r, g, b, a },

    over = { r, g, b, a }

)

There is built in support for this but i do not believe it is documented. This won’t work if the tableView is masked, as Rob already pointed out.

Ok thanks I will try that, it is masked so I guess I will loop over the rows and change the color for the highlighted one (with a custom flag) and reset the others on the event. 

For the looping codes, should I put that on the row touch event or or the row render one?

Thank you again

Try the setRowColor function in your row touch event. 

This my code on the onRowTouch event, now the row stays selected (good) but the loop doesn’t change the old one to white, any ideas?

local function onRowTouch( event )

    

   local phase = event.phase

    for i = 1, tableView:getNumRows() do

        tableView._view._rows[i]._rowColor.default = {255, 255, 255}

        tableView._view._rows[i]._rowColor.over = {255, 255, 255}

     end

   if ( “press” == phase ) then

          event.target:setRowColor{

           default = { 19, 144, 255 } ,

           over = { 19, 144, 255 }

         }

   end

end

Does this work? (untested code)

ocal function onRowTouch( event ) local phase = event.phase for i = 1, tableView:getNumRows() do tableView.\_view.\_rows[i]:setRowColor( { default = {255, 255, 255}, over = {255,255,255}} ) end if ( "press" == phase ) then event.target:setRowColor{ default = { 19, 144, 255 } , over = { 19, 144, 255 } } end end

I tried that and I have the following error:
 
attempt to call method ‘setRowColor’ (a nil value)

local function onRowTouch( event )     local phase = event.phase     local row = event.target           for i = 1, list:getNumRows() do         list.\_view.\_rows[i].\_rowColor.default = {255, 255, 255}         list.\_view.\_rows[i].\_rowColor.over = {math.random(255), math.random(255), math.random(255)}      end          if "press" == phase then     elseif "release" == phase then         -- operation     end end  

or on create row using this code:

list:insertRow{             height = 72,             rowColor = {                      default = { 255, 255, 255},                     over = { 255, 0, 0},             }, }   

Hi,
I’m having the same problem, I pasted my code below. 

local function onRowTouchGroup( event ) local phase = event.phase local row = event.target if "release" == phase then for i = 1, groupList:getNumRows() do local tmpRow = groupList:getRowAtIndex(i) tmpRow:setRowColor{ default = { 80/255 }, over = { 1, 113/255, 0 } } end row:setRowColor{ default = { 1, 113/255, 0 }, over = { 1, 113/255, 0 } } studentList:deleteAllRows() selectedGroup = row.index for i = 1, #connector.groups[row.index].students do studentList:insertRow{ rowHeight = 60, } end end end

When I click a row it is highlighted and then turns back to the default colour. but when I click on it two times fast it turns orange. When i click another item it also turns orange but the previous one isn’t being changed to the normal color.

[quote=“rick.slot,post:18,topic:319256”]

Hi,
I’m having the same problem, I pasted my code below. 

local function onRowTouchGroup( event ) local phase = event.phase local row = event.target if "release" == phase then for i = 1, groupList:getNumRows() do local tmpRow = groupList:getRowAtIndex(i) tmpRow:setRowColor{ default = { 80/255 }, over = { 1, 113/255, 0 } } end row:setRowColor{ default = { 1, 113/255, 0 }, over = { 1, 113/255, 0 } } studentList:deleteAllRows() selectedGroup = row.index for i = 1, #connector.groups[row.index].students do studentList:insertRow{ rowHeight = 60, } end end end

When I click a row it is highlighted and then turns back to the default colour. but when I click on it two times fast it turns orange. When i click another item it also turns orange but the previous one isn’t being changed to the normal color. [/quote]

Tell me what you want to do?

I want to keep one row highlighted when it is clicked. It must be deselected when i click another row, and then that row needs to be highlighted!