Multi Section TableView

Hello,

I am developing an app and I need to know hoe to display the table in groups.

I did enter one line in the array with the section title, but I think there must be another way.

groupA[1] = { flag="flags/clear.png", country="Group A" } groupA[2] = { flag="flags/ksa.png", country="Saudi Arabia" } groupA[3] = { flag="flags/egy.png", country="Egypt" } groupA[4] = { flag="flags/uru.png", country="Uruguai" } groupA[5] = { flag="flags/uru.png", country="Uruguai" } --and I add them to the list for i = 1, #groupA do       myList:insertRow{          rowHeight = 60,          isCategory = false,          rowColor = { 1, 1, 1 },          lineColor = { 0.90, 0.90, 0.90 },          params = {             flag = groupA[i].flag,             country = groupA[i].country          }       }    end

Is there another widget that I can use to group TableViews ?

I already did a tableview , but I need to break it in groups.

What I wan to display is:

Group A

Image1 Text  

image2 Text

Image3 Text

Image4 Text

Group B

Image5 Text

image6 Text

Image7 Text

Image8 Text

  

Group N

ImageNText

imageN+1 Text

ImageN+2 Text

ImageN+3 Text

TableViews support grouping by setting the isCategory value to true for your divider rows. I don’t know how you’re going to insert multiple groups. That is are you going to use nested for loops or individual for loops for each group. But using your example:

myList:insertRow { rowHeight = 60, isCategory = true, rowColor = { 0.8, 0.8, 0.8 }, lineColor = { 0.90, 0.90, 0.90 }, params = { title = "Group A", } } for i = 1, #groupA do myList:insertRow{ rowHeight = 60, isCategory = false, rowColor = { 1, 1, 1 }, lineColor = { 0.90, 0.90, 0.90 }, params = { flag = groupA[i].flag, country = groupA[i].country } } end

Then in your onRowRender function, you can test to see if it’s a category row and display it differently. You can look at the WidgetDemo sample app for an example.

Rob

Rob,

Thank you for your answer.

I tried your suggestion and I am getting an error in my onRowRender:

Attempt to index field ‘flag’ (a nil value)

 

for some reason it is not finding the params.flag.

If I comment the insert Row title from scene:Show and from onRowRender , it works fine.

function scene:show( event )     local sceneGroup = self.view          params = event.params     print("scene show " .. #groupA)     myList:insertRow {          rowHeight = 60,          isCategory = true,          rowColor = { 0.8, 0.8, 0.8 },          lineColor = { 0.90, 0.90, 0.90 },          params = {             title = "Group A",          } }     for i = 1, #groupA do       myList:insertRow{          rowHeight = 60,          isCategory = false,          rowColor = { 1, 1, 1 },          lineColor = { 0.90, 0.90, 0.90 },          params = {             flag = groupA[i].flag,             country = groupA[i].country          }       }    end end local function onRowRender( event )    --Set up the localized variables to be passed via the event table  --print ("onRowRender")    local row = event.row    local id = row.index    local params = event.row.params    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 )    if id \> #groupA then return true end    if ( event.row.params ) then                  row.titleText = display.newText( params.title, 12, 0, native.systemFontBold, 20 )       row.titleText.anchorX = 0       row.titleText.anchorY = 0.5       row.titleText:setFillColor( 0 )       row.titleText.y = 20       row.titleText.x = 42        row.flag = display.newImageRect( params.flag,70, 46 )     row.flag.anchorX=0    row.flag.anchorY=0    row.flag.x= 0    row.flag.y = row.height \*0.5-row.flag.height\*0.5       row.countryText = display.newText( params.country, 12, 0, native.systemFont, 18 )       row.countryText.anchorX = 0       row.countryText.anchorY = 1.0--0.5       row.countryText:setFillColor( 0.5 )       row.countryText.y = 40       row.countryText.x = 75       row.rightArrow = display.newImageRect(myApp.icons, 15 , 40, 40)       row.rightArrow.x = display.contentWidth - 20       row.rightArrow.y = row.height / 2       row:insert( row.rightArrow )       row:insert( row.countryText )       row:insert( row.flag )           end    return true end

eThat’s right. On the first insert row, I suggested only passing in a value for something called “title”, since that will be a category row. For that row, you’re not passing in the values of “flag” and “country”.  You have to put an “if” statement in your onRowRender that determines if it is a category row or a data row and then add the objects appropriate for that type:

if params.title then -- is category row       row.title = display.newText( params.title, 12, 0, native.systemFontBold, 20 ) else        -- code for a normal rows here end

Hello Rob,

That worked, thank you.

Now I would like to know why the group title does not scroll like the rest of the TableView? it stays at the top.

You are not inserting row.titleText into row.

The behavior of category rows is based on the iOS behavior which freezes that category at the top while items in that category are still being displayed.

Rob

TableViews support grouping by setting the isCategory value to true for your divider rows. I don’t know how you’re going to insert multiple groups. That is are you going to use nested for loops or individual for loops for each group. But using your example:

myList:insertRow { rowHeight = 60, isCategory = true, rowColor = { 0.8, 0.8, 0.8 }, lineColor = { 0.90, 0.90, 0.90 }, params = { title = "Group A", } } for i = 1, #groupA do myList:insertRow{ rowHeight = 60, isCategory = false, rowColor = { 1, 1, 1 }, lineColor = { 0.90, 0.90, 0.90 }, params = { flag = groupA[i].flag, country = groupA[i].country } } end

Then in your onRowRender function, you can test to see if it’s a category row and display it differently. You can look at the WidgetDemo sample app for an example.

Rob

Rob,

Thank you for your answer.

I tried your suggestion and I am getting an error in my onRowRender:

Attempt to index field ‘flag’ (a nil value)

 

for some reason it is not finding the params.flag.

If I comment the insert Row title from scene:Show and from onRowRender , it works fine.

function scene:show( event )     local sceneGroup = self.view          params = event.params     print("scene show " .. #groupA)     myList:insertRow {          rowHeight = 60,          isCategory = true,          rowColor = { 0.8, 0.8, 0.8 },          lineColor = { 0.90, 0.90, 0.90 },          params = {             title = "Group A",          } }     for i = 1, #groupA do       myList:insertRow{          rowHeight = 60,          isCategory = false,          rowColor = { 1, 1, 1 },          lineColor = { 0.90, 0.90, 0.90 },          params = {             flag = groupA[i].flag,             country = groupA[i].country          }       }    end end local function onRowRender( event )    --Set up the localized variables to be passed via the event table  --print ("onRowRender")    local row = event.row    local id = row.index    local params = event.row.params    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 )    if id \> #groupA then return true end    if ( event.row.params ) then                  row.titleText = display.newText( params.title, 12, 0, native.systemFontBold, 20 )       row.titleText.anchorX = 0       row.titleText.anchorY = 0.5       row.titleText:setFillColor( 0 )       row.titleText.y = 20       row.titleText.x = 42        row.flag = display.newImageRect( params.flag,70, 46 )     row.flag.anchorX=0    row.flag.anchorY=0    row.flag.x= 0    row.flag.y = row.height \*0.5-row.flag.height\*0.5       row.countryText = display.newText( params.country, 12, 0, native.systemFont, 18 )       row.countryText.anchorX = 0       row.countryText.anchorY = 1.0--0.5       row.countryText:setFillColor( 0.5 )       row.countryText.y = 40       row.countryText.x = 75       row.rightArrow = display.newImageRect(myApp.icons, 15 , 40, 40)       row.rightArrow.x = display.contentWidth - 20       row.rightArrow.y = row.height / 2       row:insert( row.rightArrow )       row:insert( row.countryText )       row:insert( row.flag )           end    return true end

eThat’s right. On the first insert row, I suggested only passing in a value for something called “title”, since that will be a category row. For that row, you’re not passing in the values of “flag” and “country”.  You have to put an “if” statement in your onRowRender that determines if it is a category row or a data row and then add the objects appropriate for that type:

if params.title then -- is category row       row.title = display.newText( params.title, 12, 0, native.systemFontBold, 20 ) else        -- code for a normal rows here end

Hello Rob,

That worked, thank you.

Now I would like to know why the group title does not scroll like the rest of the TableView? it stays at the top.

You are not inserting row.titleText into row.

The behavior of category rows is based on the iOS behavior which freezes that category at the top while items in that category are still being displayed.

Rob