tableView order

I have a tableView and how to see in the image the date is from the smallest date to the largest and I wanted to know how I can reverse this for the more recent things that I put staying in the afternoon from above and the older ones on the bottom[lua]

local function onRowRender( event )-- metodo para preencher o tableView
–Set up the localized variables to be passed via the event table
local row = event.row
local id = row.index
row.bg = display.newRect( 0, 0, display.contentWidth, 60 )
row.bg.anchorX = 0
row.bg.anchorY = 0
row.bg:setFillColor( 1, 1, 1 )
row:insert( row.bg )

row.data = display.newText("data: "… tabelaDaPublicacao[id].data, 12, 0, native.systemFontBold, 18 )
row.data.anchorX = 0
row.data.anchorY = 0.5
row.data:setFillColor( 0 )
row.data.y = 15
row.data.x = 42

row.localidade = display.newText("local: "…tabelaDaPublicacao[id].localidade, 12, 0, native.systemFont, 18 )
row.localidade.anchorX = 0
row.localidade.anchorY = 0.5
row.localidade:setFillColor( 0.5 )
row.localidade.y = 33
row.localidade.x = 42

local options =
{
text = "descricao: "… tabelaDaPublicacao[id].descricao ,
x = 12,
y = 0,
width = 250,
height = 58,
font = native.systemFont,
fontSize = 17,
align = “left” – Alignment parameter
}
row.descricao = display.newText(options)
row.descricao.anchorX = 0
row.descricao.anchorY = 0.5
row.descricao:setFillColor( 0.5 )
row.descricao.y = 70
row.descricao.x = 42

row.status = display.newText("status: " …tabelaDaPublicacao[id].status, 12, 0, native.systemFont, 18 )
row.status.anchorX = 0
row.status.anchorY = 0.5
row.status:setFillColor( 0.5 )
row.status.y = 115
row.status.x = 42

row:insert( row.data )
row:insert( row.localidade )
row:insert( row.descricao )
row:insert( row.status )–
return true
end[/lua]

Method that receives the table with the information

[lua]
function recebeTabelaDaPublicacao(tabela) – metodo que recebe a tabela de publicacao e define o tamanho de cada campo do tableView

tabelaDaPublicacao = tabela
for dis,dat in ipairs(tabela) do
–print(dis … “-” … dat.data)
myList:insertRow{
rowHeight = 125,
isCategory = false,
rowColor = { 1, 0.2, 0.1 },
lineColor = { 1, 0.20, 0.70 }}
end
end[/lua]

Tableview creation

[lua]function scene:show(event)
myList = widget.newTableView {
top = 40,
width = display.contentWidth,
height = ButtonProfile.y - 70,—display.contentHeight * 0.80,
HideBackground = true ,
onRowRender = onRowRender,
onRowTouch = onRowTouch,
listener = scrollListener
}
grupoCena:insert( myList)
end[/lua]

Hi there,
You current date order in table seems in ascending order by date.

If you want it to be sorted based on the time they are put/created.

then you just need to sort the array before creating the table view, I mean the array which holds data for you should be sorted based on time.

You might be getting the time for each row also.
Referring the UTC time would be an easy/feasible option for the row.

It would be more feasible to sort if u get UTC for each row, thus you can sort the rows based on time.

So you main array would be sorted two times.
1- based on date
2- based on UTC time
 

-Assif

 

Thank you, my friend, I understand. Sorry my ignorance is because I do not know how to do it. How should I do?

https://docs.coronalabs.com/api/library/table/sort.html

Hi andersonfseabra3,
Can u share you array that holds the complete detail for each row.
Such that I can suggest you a better solution for sorting it.

Kindly
Assif

 

The widget.newTableView() is just that a “view”.  The function onRowRender() shouldn’t know or care about data order. It should just render the one row it supposed to render.

You should manage the order that the tableView shows by the order you insert the records. The best way to do this is when you insert a row, pass the data for that row along with the insert row. In other words keep your data in a Lua table, sort it the way you want it and then insert each lua table row into the tableView. This may require you deleting all the rows in the tableView and re-inserting them, but that shouldn’t take too long to do.

If you look at:   https://docs.coronalabs.com/api/type/TableViewWidget/insertRow.html

You will see you can pass a table named “params” in to the tableView’s insert command. Then onRowRender will get a parameter:  event.row.params and you can use that data to either render the row or look up data in your data table:

local data = {} data[1] = { name = "Fred", occupation = "Rock Miner", age = 32 } data[2] = { name = "Dino", occupation = "pet", age = 5 } data[3] = { name = "Betty", occupation = "House wife", age = 27 } for i = 1, #data do      yourTableView:insertRow(        {            isCategory = isCategory,             rowHeight = rowHeight,             rowColor = rowColor,             lineColor = lineColor,             params = { data = data[i] }  -- Include custom data in the row        }) end

Now in onRowRender you can do:

local function onRowRender( event )        local row = event.row        local params = event.row.params        local thisData = params.data        row.name = display.newText(thisData.name, 5, 15, system.nativeFontBold, 15)        row:insert( row.name)        -- ... etc. end

This way onRowRender has no knowledge of your data table’s order. It just knows what data you told it to show for that specific row.

Rob

Hi there,
You current date order in table seems in ascending order by date.

If you want it to be sorted based on the time they are put/created.

then you just need to sort the array before creating the table view, I mean the array which holds data for you should be sorted based on time.

You might be getting the time for each row also.
Referring the UTC time would be an easy/feasible option for the row.

It would be more feasible to sort if u get UTC for each row, thus you can sort the rows based on time.

So you main array would be sorted two times.
1- based on date
2- based on UTC time
 

-Assif

 

Thank you, my friend, I understand. Sorry my ignorance is because I do not know how to do it. How should I do?

https://docs.coronalabs.com/api/library/table/sort.html

Hi andersonfseabra3,
Can u share you array that holds the complete detail for each row.
Such that I can suggest you a better solution for sorting it.

Kindly
Assif

 

The widget.newTableView() is just that a “view”.  The function onRowRender() shouldn’t know or care about data order. It should just render the one row it supposed to render.

You should manage the order that the tableView shows by the order you insert the records. The best way to do this is when you insert a row, pass the data for that row along with the insert row. In other words keep your data in a Lua table, sort it the way you want it and then insert each lua table row into the tableView. This may require you deleting all the rows in the tableView and re-inserting them, but that shouldn’t take too long to do.

If you look at:   https://docs.coronalabs.com/api/type/TableViewWidget/insertRow.html

You will see you can pass a table named “params” in to the tableView’s insert command. Then onRowRender will get a parameter:  event.row.params and you can use that data to either render the row or look up data in your data table:

local data = {} data[1] = { name = "Fred", occupation = "Rock Miner", age = 32 } data[2] = { name = "Dino", occupation = "pet", age = 5 } data[3] = { name = "Betty", occupation = "House wife", age = 27 } for i = 1, #data do      yourTableView:insertRow(        {            isCategory = isCategory,             rowHeight = rowHeight,             rowColor = rowColor,             lineColor = lineColor,             params = { data = data[i] }  -- Include custom data in the row        }) end

Now in onRowRender you can do:

local function onRowRender( event )        local row = event.row        local params = event.row.params        local thisData = params.data        row.name = display.newText(thisData.name, 5, 15, system.nativeFontBold, 15)        row:insert( row.name)        -- ... etc. end

This way onRowRender has no knowledge of your data table’s order. It just knows what data you told it to show for that specific row.

Rob