newTableView onRowTouch event.x is nil

Just sharing my experience: I have something like 50 rows and 4 touch listeners per row. Performance is fine for that many rows at least.

I assume my listeners are cleaned up automatically, I have not noticed any leaks at least. But you got me a bit worried :slight_smile:

IMHO its easier just to add a listener on an object instead of checking the location of the objects on touch. But in any case you can probably use a tableViewListener, I’m using that to detect sliding:

[lua]local function tableViewListener(event)

    

    local phase = event.phase

    local t = event.target

    local rowNumber = t.index

    print(event.x, event.y, phase, rowNumber)

end

local tableView = widget.newTableView {

    etc…

    listener = tableViewListener,

}[/lua]

Thanks for your answer jonjonsson,

If I use the tableViewListener as you show in the sample code, then I can know the x position but I lost the rowNumber (it is nil).

So . I will try it with touch handlers for any object…

Josep Alemany

Np. I’m getting  rowNumber from the tableViewListener without problem though. I just verified just to be sure.

Then, I don’t know what I’m doing wrong… 

To test your code, I added the listener to my tableView (list) and the tableViewListener 

        local function tableViewListener(event)

            local phase = event.phase

            local t = event.target

            local rowNumber = t.index

            

            print (’ — TableView Listener —  ')

            print(event.x, event.y, phase, rowNumber)

        end

(…)

        list = widget.newTableView

        {

          top = 121,

          left = 0,

          width = display.contentWidth, 

          height = 570,

          maskFile = “mask1280.png”,

          onRowRender = onRowRender,

          onRowTouch = onRowTouch,

          alpha=1,

        }

        list:addEventListener( “tap”, tableViewListener )

It always return phase and rowNumber nil:

RESULTS:

"— TableView Listener —  

775 282 nil nil "

Josep Alemany

The tableViewListener should be in the  list = widget.newTableView parameters.

Runnable sample:

[lua]display.setStatusBar( display.HiddenStatusBar )

local widget = require( “widget” )

local tableView

local function tableViewListener(event)

    

    local phase = event.phase

    local t = event.target

    local rowNumber = t.index

    print(event.x, event.y, phase, rowNumber)

end

local function onRowRender( event )

    local rowTitle = display.newText( event.row, “Row”, display.contentWidth/2, 40 * 0.5, nil, 14 )

    rowTitle:setFillColor(0, 0, 0)

end

tableView = widget.newTableView {

    onRowRender = onRowRender,

    listener = tableViewListener,

}

tableView:insertRow{}

tableView:insertRow{}

tableView:insertRow{}[/lua]

Ok, that’s the problem. I do it so quickly… and makes an error

Now it works fine  :slight_smile:

Thanks you very much for your help and patience.

Josep Alemany

Amazing! I learn so much here everyday! Thanks much for sharing so willingly.

Great Josep! No problem Kerem, you are the share king!

The performance shouldn’t be hurt at all.  Given the following 100 rows, 4 touchable objects per row, I would assume that would me 4 functions total, one for each of the four touchable items.  Functions take up very little in the way of memory, so no impact there (and you probably already have the code in place to handle the touches anyway,  Next, handlers themselves are just a function pointer on the object itself.  When a touch happens, we look to see if there is a display object where you touched and look up its touch handler if it has one and executes it.

Worst case scenario is trying to remove the touch handler (which doesn’t need to happen since you can’t interact with a touch handler on an object that’s offscreen or gone anyway and like I said, its just a pointer in the object itself) where it would have to iterate over a table looking for it’s signature (event type, function pointer).  How long does it take to run this block of code (remember 400 total handlers in the table)

for i = 1, 400 do

    if myEvent.function = listofhandlers[i].function and myEvent.type = listofhandlers[i].type then

          table.remove(listofhandlers[i])

          break

    end

end

Probably would happen in less than a frame’s cycle of work.  Adding the event just tacks it on the to the end of the table which is rather quick.

This is how Corona’s event system works.  I like to say “Don’t fight the system” and trying to calculate a position in the row to figure out where your touching is going to be just as code/resource costly as doing the touch events.

Rob