Is there a way to achieve the scrollView:takeFocus() feature for a tableView?

Hello,

Is there a way to achieve the same effect scrollView:takeFocus has but with a tableView? I currently have a rect placed on top of my tableView and I’m trying to pass the touchFocus on to the tableView if the event.phase of the rect is ‘moved’. My current code is like so but it’s not working:

-- Touch listener  
local function touchListener(event)  
 local phase = event.phase  
 local x = event.x - event.xStart  
 local y = event.y - event.yStart  
  
 if(phase == 'began') then  
 --[[  
 if(y \> 5) then  
 return false  
 end  
 --]]  
 elseif(phase == 'moved') then  
 if(y \> 5) then  
 --display.getCurrentStage():setFocus(tableView)  
 --display.getCurrentStage():setFocus(tableView.content)  
 --event.target:dispatchEvent({name='touch', phase='began', y=12, yStart=6, x=0, xStart=0})  
 return false  
 end  
 end  
  
 if(y \<= 5 and phase == 'moved' or phase ~= 'moved') then  
 return true  
 end  
end  
  
-- Touch rect  
local touchRect = display.newRect(0, 0, 60, 480)  
touchRect:setFillColor(255, 0, 0)  
touchRect:addEventListener('touch', touchListener)  
group:insert(touchRect)  

I’ve tried dispatching events manually & setting the stage focus on the tableView and tried returning false but neither solution seems to be working… Does anyone know if this is possible to achieve?

Thanks! [import]uid: 14018 topic_id: 34203 reply_id: 334203[/import]

I thought tableView supported that call. Have you tried to call tableView:takeFocus()? Let me ask the team, perhaps the docs need updated.

[import]uid: 199310 topic_id: 34203 reply_id: 136077[/import]

Gah, amazing :slight_smile: Seems like I was trying tableView.content:takeFocus() for some reason. Did the normal way and it’s working beautifully now. Thanks a lot! [import]uid: 14018 topic_id: 34203 reply_id: 136081[/import]

I thought tableView supported that call. Have you tried to call tableView:takeFocus()? Let me ask the team, perhaps the docs need updated.

[import]uid: 199310 topic_id: 34203 reply_id: 136077[/import]

Gah, amazing :slight_smile: Seems like I was trying tableView.content:takeFocus() for some reason. Did the normal way and it’s working beautifully now. Thanks a lot! [import]uid: 14018 topic_id: 34203 reply_id: 136081[/import]

I’ve tried calling tableView:takeFocus() but it is not working. I realize that this thread is quite old. Has something changed about this method?

I’ve tried calling tableView:takeFocus() but it is not working. I realize that this thread is quite old. Has something changed about this method?

Running into the same issue here. I’m trying to pass the touch event from a button in the table view, to the table view itself on the “moved” event. Has anyone got this to work? Using build 2015.2714

Hi @Appduction Studios,

I don’t believe anything has changed on this API. As always, posting some relevant code would be very helpful. :slight_smile:

Brent

I seem to remember running into this issue.

Within the ‘moved’ phase of your button, try:

[lua]

display.getCurrentStage():setFocus(nil)

event.target = tableView._view

event.phase = “began”

tableView._view.touch(tableView._view, event)

[/lua]

@Brent, sure thing. I’ve stripped down the code to a very basic example. I also print out the table_view after creation and in the moved event to ensure that I’m referencing the same table. It crashes with “Attempt to call method takeFocus a nil value”.

@nick_sherman, your example works, thanks for the quick reply. However, upon scrolling, the button default and over colours don’t change, so I’m checking how I can trigger that manually.

local table\_view local function onRowRender(event) local row = event.row local row\_height = row.contentHeight local row\_width = row.contentWidth local function buttonHandler(event) if event.phase == "moved" then print(table\_view) table\_view:takeFocus(event) end end local test\_button = widget.newButton { x = row\_width \* 0.5, y = row\_height \* 0.5, shape = "rect", width = 100, height = 50, label = "Test Button", onEvent = buttonHandler, fillColor = { default = {0, 0, 0, 1}, over = {1, 1, 1, 1} } } row:insert(test\_button) end table\_view = widget.newTableView { left = 0, top = 0, width = display.contentWidth, height = display.contentHeight, hideBackground = true, onRowRender = onRowRender } print(table\_view) scene\_group:insert(table\_view) for i=1, 10 do table\_view:insertRow { rowHeight = 100 } end

Ah - yeah I don’t use widget buttons so wouldn’t have that issue.

I see from this thread that takeFocus was never implemented on tableView, only scrollView?

https://forums.coronalabs.com/topic/33579-info-scrollview-take-focus-is-coming-back/

Same problem here… 

attempt to call method 'takeFocus' (a nil value)

PS: @nick_sherman solution worked. Thanks @nick_sherman.

Running into the same issue here. I’m trying to pass the touch event from a button in the table view, to the table view itself on the “moved” event. Has anyone got this to work? Using build 2015.2714

Hi @Appduction Studios,

I don’t believe anything has changed on this API. As always, posting some relevant code would be very helpful. :slight_smile:

Brent

I seem to remember running into this issue.

Within the ‘moved’ phase of your button, try:

[lua]

display.getCurrentStage():setFocus(nil)

event.target = tableView._view

event.phase = “began”

tableView._view.touch(tableView._view, event)

[/lua]

@Brent, sure thing. I’ve stripped down the code to a very basic example. I also print out the table_view after creation and in the moved event to ensure that I’m referencing the same table. It crashes with “Attempt to call method takeFocus a nil value”.

@nick_sherman, your example works, thanks for the quick reply. However, upon scrolling, the button default and over colours don’t change, so I’m checking how I can trigger that manually.

local table\_view local function onRowRender(event) local row = event.row local row\_height = row.contentHeight local row\_width = row.contentWidth local function buttonHandler(event) if event.phase == "moved" then print(table\_view) table\_view:takeFocus(event) end end local test\_button = widget.newButton { x = row\_width \* 0.5, y = row\_height \* 0.5, shape = "rect", width = 100, height = 50, label = "Test Button", onEvent = buttonHandler, fillColor = { default = {0, 0, 0, 1}, over = {1, 1, 1, 1} } } row:insert(test\_button) end table\_view = widget.newTableView { left = 0, top = 0, width = display.contentWidth, height = display.contentHeight, hideBackground = true, onRowRender = onRowRender } print(table\_view) scene\_group:insert(table\_view) for i=1, 10 do table\_view:insertRow { rowHeight = 100 } end

Ah - yeah I don’t use widget buttons so wouldn’t have that issue.

I see from this thread that takeFocus was never implemented on tableView, only scrollView?

https://forums.coronalabs.com/topic/33579-info-scrollview-take-focus-is-coming-back/

Same problem here… 

attempt to call method 'takeFocus' (a nil value)

PS: @nick_sherman solution worked. Thanks @nick_sherman.