onRowTouch returns same row id after the first touch

i have my tableView set and when i click on the row i get say id of 4. When i click on another row i get the same id. It only return a correct id the first time

local onRowTouch = function( event )
    if event.phase == “release” then
        
        local id = event.row.index

        local params = {
            contentType = event.row.contentType,
            --resourceID = event.row.resourceID,
            resourceID = event.row.index,
            eventTitle = event.row.eventTitle
        }
        composer.gotoScene(“viewDetails”, {time=250, effect=“crossFade”, params = params})
    end
    return true
end

Can you post your whole onRowTouch function?

Rob

Sorry.I have edited my post. Thanks

Looks like you should be accessing event. target.index

http://docs.coronalabs.com/api/library/widget/newTableView.html#onrowtouch-optional

i changed it to event.target.index but still no change.

I just did a test using the Business App Sample which uses tableViews and I’m getting the proper value for these.   I also added a print to the onRowTouch function of the ListView1 sample app, and it’s also printing the right values.  You can conduct the same test if you like.

This points to the possibility that somewhere in  your code you’re overwriting the value.  Perhaps its in your onRowRender() function or when you’re inserting the items into the table.

I’d look there.  If you need help perhaps you could post those blocks of code as well.

Rob

Rob,

So if I comment out the line below and just print the id it works fine on successive row clicks.

composer.gotoScene(“viewDetails”, {time=250, effect=“crossFade”, params = params}

The issue arises when i switch to my “viewDetails” scene. When i go back to the scene with tableView that’s when it always returns the first clicked id.

Thanks

Here is the whole code for the scene:

----------------------------------------------------------------------------------------- -- -- view1.lua -- ----------------------------------------------------------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() local widget = require( "widget" ) local mime = require("mime") local json = require("json") local tableView = nil local URL = "" -- i took the URL out local function apiCallback(event) if ( event.isError ) then print( "Network error!") else data = json.decode(event.response) local rowData = data.data for key,value in pairs(rowData) do --actualcode print(key .. "---" .. value.title) tableView:insertRow{ rowHeight = 90, params = { title = value.title, eventDate = value.datetime\_utc, venue = value.venue.city, resourceID = value.event\_id } -- Include custom data in the row } end end end local function detailsCallback(event) if ( event.isError ) then print( "Network error!") else data = json.decode(event.response) eventTitle = "testagsjahsjahjsas" end end -- -- this function gets called when we tap on a row. -- local onRowTouch = function( event ) if event.phase == "release" then local id = event.target.index --params = event.row.contentType -- get details here --showDetails(event.row.resourceID) local params = { contentType = event.row.contentType, resourceID = event.row.resourceID, --resourceID = event.target.index, eventTitle = event.row.eventTitle } print("ID is " .. event.row.eventTitle) composer.gotoScene("viewDetails", {time=250, effect="crossFade", params = params}) end return true end local function onRowRender(event) local row = event.row row.img = display.newImage("Icon.png") row.img.x = math.floor(row.img.width\*0.5 + 6) row.img.y = math.floor(row.img.height\*0.5 + 10) row:insert(row.img) row.title = display.newText( row.params.title, 12, 0, 250, 0, "Verdana", 12 ) row.title.anchorX = 0 row.title.anchorY = 0.5 row.title:setFillColor( 0 ) row.title.y = 22 row.title.x = row.img.width + 10 row:insert(row.title ) local options = { --parent = textGroup, --text = "SAT, JUN 21 @ 7:30 AM", text = row.params.venue, x = 0, y = row.title.height + row.title.y + 6, width = display.contentWidth, --required for multi-line and alignment font = "Verdana", fontSize = 10, align = "center" --new alignment parameter } row.venue = display.newText(options) row.venue:setFillColor( 0) row.venue.anchorX = 0 row:insert(row.venue) local options = { --parent = textGroup, --text = "SAT, JUN 21 @ 7:30 AM", text = row.params.eventDate, x = 0, y = 75, width = display.contentWidth, --required for multi-line and alignment font = "Verdana", fontSize = 10, align = "center" --new alignment parameter } row.eventDate = display.newText(options) row.eventDate:setFillColor( 0) row.eventDate.anchorX = 0 row:insert(row.eventDate) row.contentType = "event" row.resourceID = row.params.resourceID row.eventTitle = row.params.title end local function purgeList(list) list:deleteAllRows() end function scene:create( event ) local sceneGroup = self.view --local background = display.newRect(0,0,display.contentWidth, display.contentHeight) local background = display.newImageRect('bg.png',900,1425) --background:setFillColor( 0.95, 0.95, 0.95 ) background:setFillColor( 196, 255, 156, 255 ) background.x = display.contentWidth / 2 background.y = display.contentHeight / 2 sceneGroup:insert(background) tableView = widget.newTableView { onRowRender = onRowRender, onRowTouch = onRowTouch, listener = scrollListener } -- Insert rows local function apiCallback(event) if ( event.isError ) then print( "Network error!") else data = json.decode(event.response) local rowData = data.data for key,value in pairs(rowData) do --actualcode print(key .. "---" .. value.title) tableView:insertRow{ rowHeight = 90, params = { title = value.title, eventDate = value.datetime\_utc, venue = value.venue.city, resourceID = value.event\_id } -- Include custom data in the row } end end end network.request( URL, "GET", apiCallback ) sceneGroup:insert(tableView) end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen elseif phase == "did" then -- Called when the scene is now on screen -- -- INSERT code here to make the scene come alive -- e.g. start timers, begin animation, play audio, etc. purgeList(tableView) network.request( URL, "GET", apiCallback ) end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) print("exit scene") purgeList(tableView) elseif phase == "did" then -- Called when the scene is now off screen end end function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc. end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) ----------------------------------------------------------------------------------------- return scene

It looks like your “params” is global in your ApiCallback function.  I don’t know if that’s causing problems or not.  It seems that the scene you’re going to is interfering with the tableView some how and globals would be the first suspect. 

Rob

So how do i go about the params being global. Below is my other scene that i go to after touching the row.

----------------------------------------------------------------------------------------- -- -- view1.lua -- ----------------------------------------------------------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() local widget = require( "widget" ) local mime = require("mime") local json = require("json") -- ScrollView listener local function scrollListener( event ) local phase = event.phase if ( phase == "began" ) then print( "Scroll view was touched" ) elseif ( phase == "moved" ) then print( "Scroll view was moved" ) elseif ( phase == "ended" ) then print( "Scroll view was released" ) end -- In the event a scroll limit is reached... if ( event.limitReached ) then if ( event.direction == "up" ) then print( "Reached top limit" ) elseif ( event.direction == "down" ) then print( "Reached bottom limit" ) elseif ( event.direction == "left" ) then print( "Reached left limit" ) elseif ( event.direction == "right" ) then print( "Reached right limit" ) end end return true end -- Create the widget local scrollView = widget.newScrollView { top = 0, left = 0, width = display.contentWidth, height = display.contentHeight, topPadding = 50, bottomPadding = 50, --scrollWidth = 600, --scrollHeight = 800, horizontalScrollDisabled = true, listener = scrollListener } function scene:create( event ) local sceneGroup = self.view local eventTitle = event.params.eventTitle local resourceID = event.params.resourceID local resourceType = event.params.contentType --local background = display.newRect(0,0,display.contentWidth, display.contentHeight) local background = display.newImageRect('bg.png',900,1425) --background:setFillColor( 0.95, 0.95, 0.95 ) background.x = display.contentWidth / 2 background.y = display.contentHeight / 2 sceneGroup:insert(background) --scrollView:insert( background ) local options = { --parent = textGroup, text = eventTitle .. " " .. resourceID, x = 0, y = 0, width = display.contentWidth, --required for multi-line and alignment font = "Verdana", fontSize = 14, align = "center" --new alignment parameter } title = display.newText(options) title.anchorX = 0 title:setFillColor( 0, 0, 0 ) sceneGroup:insert(title) scrollView:insert( title ) resourceContent = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum." local options = { --parent = textGroup, text = "SUNDAY, JUN 1 @ 10:00 AM - 6:00 PM", x = 0, y = 42, width = display.contentWidth, --required for multi-line and alignment font = "Verdana", fontSize = 10, align = "center" --new alignment parameter } local eventDate = display.newText(options) eventDate:setFillColor(0.22, 0.40, 0.55) eventDate.anchorX = 0 sceneGroup:insert(eventDate) scrollView:insert( eventDate ) local txt\_about = display.newText( resourceContent, 0, 0, 280, 0, "Verdana", 12 ) txt\_about:setTextColor( 22, 22, 22 ) txt\_about.x = 155 txt\_about.y = 330 txt\_about:setFillColor( 0, 0, 0 ) sceneGroup:insert(txt\_about) scrollView:insert( txt\_about ) sceneGroup:insert(scrollView) end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen elseif phase == "did" then -- Called when the scene is now on screen -- -- INSERT code here to make the scene come alive -- e.g. start timers, begin animation, play audio, etc. end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) elseif phase == "did" then -- Called when the scene is now off screen end end function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc. end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) ----------------------------------------------------------------------------------------- return scene

Actually you don’t want params to be global.  I didn’t see anything obvious.  One thing you might want to do is call composer.removeScene() before you call composer.gotoScene() to make sure you remove the scene before you go back to it.

Rob

ok thanks. Yes i tried to remove the scene before and I tried but no lucky.

Can you post your whole onRowTouch function?

Rob

Sorry.I have edited my post. Thanks

Looks like you should be accessing event. target.index

http://docs.coronalabs.com/api/library/widget/newTableView.html#onrowtouch-optional

i changed it to event.target.index but still no change.

I just did a test using the Business App Sample which uses tableViews and I’m getting the proper value for these.   I also added a print to the onRowTouch function of the ListView1 sample app, and it’s also printing the right values.  You can conduct the same test if you like.

This points to the possibility that somewhere in  your code you’re overwriting the value.  Perhaps its in your onRowRender() function or when you’re inserting the items into the table.

I’d look there.  If you need help perhaps you could post those blocks of code as well.

Rob

Rob,

So if I comment out the line below and just print the id it works fine on successive row clicks.

composer.gotoScene(“viewDetails”, {time=250, effect=“crossFade”, params = params}

The issue arises when i switch to my “viewDetails” scene. When i go back to the scene with tableView that’s when it always returns the first clicked id.

Thanks

Here is the whole code for the scene:

----------------------------------------------------------------------------------------- -- -- view1.lua -- ----------------------------------------------------------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() local widget = require( "widget" ) local mime = require("mime") local json = require("json") local tableView = nil local URL = "" -- i took the URL out local function apiCallback(event) if ( event.isError ) then print( "Network error!") else data = json.decode(event.response) local rowData = data.data for key,value in pairs(rowData) do --actualcode print(key .. "---" .. value.title) tableView:insertRow{ rowHeight = 90, params = { title = value.title, eventDate = value.datetime\_utc, venue = value.venue.city, resourceID = value.event\_id } -- Include custom data in the row } end end end local function detailsCallback(event) if ( event.isError ) then print( "Network error!") else data = json.decode(event.response) eventTitle = "testagsjahsjahjsas" end end -- -- this function gets called when we tap on a row. -- local onRowTouch = function( event ) if event.phase == "release" then local id = event.target.index --params = event.row.contentType -- get details here --showDetails(event.row.resourceID) local params = { contentType = event.row.contentType, resourceID = event.row.resourceID, --resourceID = event.target.index, eventTitle = event.row.eventTitle } print("ID is " .. event.row.eventTitle) composer.gotoScene("viewDetails", {time=250, effect="crossFade", params = params}) end return true end local function onRowRender(event) local row = event.row row.img = display.newImage("Icon.png") row.img.x = math.floor(row.img.width\*0.5 + 6) row.img.y = math.floor(row.img.height\*0.5 + 10) row:insert(row.img) row.title = display.newText( row.params.title, 12, 0, 250, 0, "Verdana", 12 ) row.title.anchorX = 0 row.title.anchorY = 0.5 row.title:setFillColor( 0 ) row.title.y = 22 row.title.x = row.img.width + 10 row:insert(row.title ) local options = { --parent = textGroup, --text = "SAT, JUN 21 @ 7:30 AM", text = row.params.venue, x = 0, y = row.title.height + row.title.y + 6, width = display.contentWidth, --required for multi-line and alignment font = "Verdana", fontSize = 10, align = "center" --new alignment parameter } row.venue = display.newText(options) row.venue:setFillColor( 0) row.venue.anchorX = 0 row:insert(row.venue) local options = { --parent = textGroup, --text = "SAT, JUN 21 @ 7:30 AM", text = row.params.eventDate, x = 0, y = 75, width = display.contentWidth, --required for multi-line and alignment font = "Verdana", fontSize = 10, align = "center" --new alignment parameter } row.eventDate = display.newText(options) row.eventDate:setFillColor( 0) row.eventDate.anchorX = 0 row:insert(row.eventDate) row.contentType = "event" row.resourceID = row.params.resourceID row.eventTitle = row.params.title end local function purgeList(list) list:deleteAllRows() end function scene:create( event ) local sceneGroup = self.view --local background = display.newRect(0,0,display.contentWidth, display.contentHeight) local background = display.newImageRect('bg.png',900,1425) --background:setFillColor( 0.95, 0.95, 0.95 ) background:setFillColor( 196, 255, 156, 255 ) background.x = display.contentWidth / 2 background.y = display.contentHeight / 2 sceneGroup:insert(background) tableView = widget.newTableView { onRowRender = onRowRender, onRowTouch = onRowTouch, listener = scrollListener } -- Insert rows local function apiCallback(event) if ( event.isError ) then print( "Network error!") else data = json.decode(event.response) local rowData = data.data for key,value in pairs(rowData) do --actualcode print(key .. "---" .. value.title) tableView:insertRow{ rowHeight = 90, params = { title = value.title, eventDate = value.datetime\_utc, venue = value.venue.city, resourceID = value.event\_id } -- Include custom data in the row } end end end network.request( URL, "GET", apiCallback ) sceneGroup:insert(tableView) end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen elseif phase == "did" then -- Called when the scene is now on screen -- -- INSERT code here to make the scene come alive -- e.g. start timers, begin animation, play audio, etc. purgeList(tableView) network.request( URL, "GET", apiCallback ) end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) print("exit scene") purgeList(tableView) elseif phase == "did" then -- Called when the scene is now off screen end end function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc. end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) ----------------------------------------------------------------------------------------- return scene

It looks like your “params” is global in your ApiCallback function.  I don’t know if that’s causing problems or not.  It seems that the scene you’re going to is interfering with the tableView some how and globals would be the first suspect. 

Rob