Adding text into a specific row in a tableview (when you create it)

Hi,

I’ve made a tableview, and the user can add to the list. The list is empty at first, but then if they press the “add” button, it takes them to a screen where they can enter into a textfield what they want to the row’s name to be. I store the input into a variable, and then I want to add that input into a new row. How can I do that?

I know that you have to change the onRowRender() function if you want the list to have already been populated:

 local rowTitle local rowText if (row.isCategory) then if row.index == 1 then rowText = "CATEGORY 1" rowTitle = display.newText(row, rowText, 0, 0, globals.font.regular, 20) rowTitle.x = constants.leftPadding else rowText = "CATEGORY " .. row.index % 10 + 1 rowTitle = display.newText(row, rowText, 0, 0, globals.font.regular, 20) end else rowText = "Row" \<-- change that to change the first letter rowTitle = display.newText(row, rowText .. row.index, 0,0, globals.font.regular, 20) end

which will make the tableView have this (let’s just say I took the category part out):

-Row 2

-Row 3

-Row 4

-Row 5

-Row 6

-Row 7

etc.

But I want the list to be empty first. Then, by user input, be populated. So instead of Row 1, Row 2, Row 3, it would be:

-Walk the Dog

-Wash the car

-Water the plants

-Finish the Lord of the Rings

etc.

How can I insert specific text into each row of a tableView?

Normally you would have a table, indexed by number like:

local strings = {}

strings[1] = “Walk the Dog”

strings[2] = “Wash the car”

etc.

You can then add new ones in to the table by doing:

strings[#strings+1] = yourtextField.text

Then when you call insertRow() to add a row and it triggers the onRowRender() function, you would do:

rowTitle = display.newText(row, strings[row.index], 0,0, globals.font.regular, 20)

But the trick here is you are using Groups.  Groups mess up the indexing since they count as rows too, so if you have a group as row 3, then when you will end up skipping that record.  The solution here is to pass an extra parameter to the
tableView:insert() function called params.  This table can have any data you want to pass that gets assigned to that row specifically.

        myList:insertRow{
            rowHeight = 60,
            isCategory = false,
            rowColor = { 1, 1, 1 },
            lineColor = { 0.90, 0.90, 0.90 },
            params = {
                rowTitle = strings[i] – assuming “i” is the row you are inserting.
            }
        }

then your onRowRender function changes to be:

rowTitle = display.newText(row, event.row.params.rowTitle, 0,0, globals.font.regular, 20)

Normally you would have a table, indexed by number like:

local strings = {}

strings[1] = “Walk the Dog”

strings[2] = “Wash the car”

etc.

You can then add new ones in to the table by doing:

strings[#strings+1] = yourtextField.text

Then when you call insertRow() to add a row and it triggers the onRowRender() function, you would do:

rowTitle = display.newText(row, strings[row.index], 0,0, globals.font.regular, 20)

But the trick here is you are using Groups.  Groups mess up the indexing since they count as rows too, so if you have a group as row 3, then when you will end up skipping that record.  The solution here is to pass an extra parameter to the
tableView:insert() function called params.  This table can have any data you want to pass that gets assigned to that row specifically.

        myList:insertRow{
            rowHeight = 60,
            isCategory = false,
            rowColor = { 1, 1, 1 },
            lineColor = { 0.90, 0.90, 0.90 },
            params = {
                rowTitle = strings[i] – assuming “i” is the row you are inserting.
            }
        }

then your onRowRender function changes to be:

rowTitle = display.newText(row, event.row.params.rowTitle, 0,0, globals.font.regular, 20)

Hi Rob,

Old post I know, but I ran into the same problem. 

Your code: rowTitle = display.newText(row, event.row.params.rowTitle, 0,0, nil, 20)   did not work for me and threw the error: bad argument #2 ‘newText’ (string expected, got nil).

I fixed it by adding tostring():

rowTitle = display.newText(row, tostring(event.row.params.rowTitle), 0,0, nil, 20)

Just letting you know.

Thanks

Hi Rob,

Old post I know, but I ran into the same problem. 

Your code: rowTitle = display.newText(row, event.row.params.rowTitle, 0,0, nil, 20)   did not work for me and threw the error: bad argument #2 ‘newText’ (string expected, got nil).

I fixed it by adding tostring():

rowTitle = display.newText(row, tostring(event.row.params.rowTitle), 0,0, nil, 20)

Just letting you know.

Thanks