Search code examples
luacoronasdkcorona-storyboard

Adding textfield in table row - Corona SDK


In my corona project i want to show textfield in table row.Here the code ,its shows only one textfield, i want to show the textfield in data available rows.

local function onRowRender( event )
local phase = event.phase
local row = event.row

local itemObj = GetItemDetails(row.index);

local txt = itemObj.itemName;
if(#txt > 25) then
    txt = string.sub(txt,1,25);
    txt = txt.."..";
end

local rowTitle = display.newText( row, "\t"..txt, 0, 0, "impact", 14 )
if(deviceName == "iPhone" or deviceName == "iPad") then
    rowTitle.x = row.x - ( row.contentWidth * 0.5 ) + ( rowTitle.contentWidth * 0.5 )
else
    rowTitle.x = row.x - ( row.contentWidth * 0.5 ) + ( rowTitle.contentWidth * 0.5 )+15
end
rowTitle.y = row.contentHeight * 0.5
rowTitle:setTextColor(98, 71, 24)

local rowqtytxt=nil; 
if(deviceName == "iPhone" or deviceName == "iPad") then
    rowqtytxt= native.newTextField( 0, 0,32, 32 ,onSubmitted);
    rowqtytxt.x = 125;
    rowqtytxt.hasBackground = false;
    --rowqtytxt:addEventListener( "userInput", textListener )
    rowqtytxt.size = 32;
else
    rowqtytxt = native.newTextField( 0, 0,32, 32 ,onSubmitted);
    rowqtytxt.x = 207;
    rowqtytxt.hasBackground = false;
   -- rowqtytxt:addEventListener( "userInput", textListener )
    rowqtytxt.size = 32;
end

rowqtytxt.y = row.contentHeight * 0.5
rowqtytxt:setTextColor(98, 71, 24)
return true;
end

The above code shows only one text field.Please help to solve this issue


Solution

  • When you say 'data available rows', what do you mean? It seems to me you mean that itemObj will not always have data in it?

    If this is the case, then you need to wrap the adding of the text field in an if block:

    if(itemObj ~= nil) then
        --Adding textField code
    end
    

    Also, this function seems to render a single row. So you would need to wrap it in a for loop. Something like

    local i;
    --The # means 'length of'
    for i=1, #yourDataSource do
        onRowRender(yourItemData)
    end