Now I want to create a similar table view but with more than one column. I wasn’t sure if I could have more than one column using a table view so I used widget.newScrollView() and displayed a list of objects in 2 separate columns (see screenshot). However, I do not know how to create a method that allows the user to tap/touch an item. I don’t know how to create/send the item id or name or any param.
Can someone advise me on the best way to approach this?
The params are useful for tableViews because tableViews have to render each row. ScrollViews don’t have such a rendering feature, they are really just a display.newGroup() with scrolling controls added. You just insert a display object in a scrollView and the scrollView will scroll the group around.
As for the best way to know what’s being tapped on, you can always add an ID to the display object:
local function touchHandlerFunction( event ) print(event.target.name) -- print's "redcar1" -- do stuff based on the .name parameter return true end local someObject = display.newImageRect("redcar1.png", 150, 50) someObject.name = "redcar1" scrollView:insert(someObject) someObject:addEventListener("touch", touchHandlerFunction)
This does what I need however the scrollview no longer scrolls. It scrolls when I comment out the return true but then the touch listener does not trigger without the return true.
So between scrolling and selecting a car, I can only get one to work but not both. I removed the listener option when I create the newScrollView.
If you detect in your touch listener that you’re trying to move the scrollView instead of interact with the item, you need to call this takeFocus() API to pass the touch to the scrollView.
The params are useful for tableViews because tableViews have to render each row. ScrollViews don’t have such a rendering feature, they are really just a display.newGroup() with scrolling controls added. You just insert a display object in a scrollView and the scrollView will scroll the group around.
As for the best way to know what’s being tapped on, you can always add an ID to the display object:
local function touchHandlerFunction( event ) print(event.target.name) -- print's "redcar1" -- do stuff based on the .name parameter return true end local someObject = display.newImageRect("redcar1.png", 150, 50) someObject.name = "redcar1" scrollView:insert(someObject) someObject:addEventListener("touch", touchHandlerFunction)
This does what I need however the scrollview no longer scrolls. It scrolls when I comment out the return true but then the touch listener does not trigger without the return true.
So between scrolling and selecting a car, I can only get one to work but not both. I removed the listener option when I create the newScrollView.
If you detect in your touch listener that you’re trying to move the scrollView instead of interact with the item, you need to call this takeFocus() API to pass the touch to the scrollView.