newTableView onRowTouch event.x is nil

This is in the 2013.2100 simulator, but I was thinking there’s a conceptual mistake on my part rather than something version specific.

In other words, I would onRowTouch to delete a row if it’s touched on the part showing a delete graphic, rather than adding (and removing, with rows deleted etc… brrr!) listeners and keeping track of buttons on each row.

I do get a proper touch cycle, so why can’t it be done this way? Example code:

-- Handle touches on the row local function onRowTouch( event ) local phase = event.phase local row = event.target local i=row.index if ("release" == phase) and (i\>0) then print("event.x:",event.x) -- prints nil     end end

There have been significant bug fixes to the tableView widget since build 2100.  I would upgrade to 2189 and resume from there.

Rob

There have been significant bug fixes to the tableView widget since build 2100.  I would upgrade to 2189 and resume from there.

Rob

I have the same problem. As I read the advice of Rob I update to the last version 2014.2340 but the problem persists :(. How can I do to discover the x position touched in a listView Row? I have 4 elements on this row and need to know which one is touched!

Thanks!

Josep Alemany

Why not add touch handlers on each object that you want touched and use that object’s touch handler?
 

Rob

Would the performance not suffer dramatically? 4 listeners per row times say 20 rows on display, lazy loading will be used to kill listeners for rows going out of view and create new ones for rows coming into view… I suspect it might not be too pleasant to scroll this one. I could be wrong of course. I often am!  :slight_smile:

For two reasons Rob:

Simplicity: it is really more easy to have only one Listener per row that is integrated with the listView. 

Performance: as ksan said, I think it will be not very efficient.

Of course, it is a solution. It is my ‘plan B’, if I can’t find the way to work with listVIew onRowTouch event and then determine the element touched with the x position…

Thanks!

Josep Alemany 

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

I have the same problem. As I read the advice of Rob I update to the last version 2014.2340 but the problem persists :(. How can I do to discover the x position touched in a listView Row? I have 4 elements on this row and need to know which one is touched!

Thanks!

Josep Alemany

Why not add touch handlers on each object that you want touched and use that object’s touch handler?
 

Rob

Would the performance not suffer dramatically? 4 listeners per row times say 20 rows on display, lazy loading will be used to kill listeners for rows going out of view and create new ones for rows coming into view… I suspect it might not be too pleasant to scroll this one. I could be wrong of course. I often am!  :slight_smile:

For two reasons Rob:

Simplicity: it is really more easy to have only one Listener per row that is integrated with the listView. 

Performance: as ksan said, I think it will be not very efficient.

Of course, it is a solution. It is my ‘plan B’, if I can’t find the way to work with listVIew onRowTouch event and then determine the element touched with the x position…

Thanks!

Josep Alemany