Adding objects to scrollView

Hi,

So I am trying to have an app where there is a button that can create new rectangular objects (representing buttons) and they are added to a scrollView. I am getting an error when I try to add the array to the scrollView. Is there another method that I can do the same thing

You haven’t posted any code so we can’t tell you what’s wrong with it.

As Lua doesn’t have arrays, I’m guessing you mean a table - so it’s possible you’re trying to add a table to a scrollview. If you read the error output, the console will tell you that cannot be done because a table is not a display object and only display objects can be added to scrollviews.

Again, if you post your code we will be able to help more.

Here is the code:


– main.lua


– Your code here

local composer = require “composer”

local scene = composer.newScene()

local widget = require “widget”

local height1 = display.contentHeight/6

local scrollView = widget.newScrollView

{

backgroundColor = {0.215, 0.67, 0.27},

left = 0,

top = 0,

width = display.contentWidth,

height = display.viewableContentHeight + 200,

topPadding = 30,

bottomPadding = 0,

horizontalScrollDisabled = false,

verticalScrollDisabled = false,

scrollHeight = display.contentHeight

}

scrollView.x = display.contentWidth/2

scrollView.y = display.contentHeight/2

–add new contact

local add = widget.newButton

{

left = 5,

top = 20,

width = 60,

height = 60,

id = “plus”,

label = “+”,

labelColor = {default = {1,1,1}, over = {1,1,1}},

fontSize = 140

}

add.x = display.contentWidth - add.width/2 - 10

add.y = add.height/4

function scene:create (event)

local homescreenGroup = self.view

–array for contacts

local contacts = {}

local numContacts = 1

local x = display.contentWidth/2

local y = -20

scrollView:insert(add)

–scrollView:insert(contacts)

homescreenGroup:insert(scrollView)

local function loadContacts()

y = y + display.contentHeight/6 + 30

contacts[numContacts] = display.newRect(x, y, display.contentWidth/1.5, display.contentHeight/6)

composer.gotoScene(“form”)

end

local function createContact()

numContacts = numContacts + 1

loadContacts()

end

add:addEventListener(“tap”, createContact)

end

function scene:hide(event)

end

function scene:show(event)

end

scene:addEventListener(“create”, scene)

scene:addEventListener(“show”, scene)

scene:addEventListener(“hide”, scene)

return scene

Is that really your main.lua? or is that just some misleading comments? you have a number of issues to sort out if so.

Assuming this is a scene file, and I’ve understood your requirement correctly, the only thing you appear to be missing is a line to add the rectangles into the scrollview in your loadContacts() function.

e.g. scrollView:insert(contacts[numContacts])

‘contacts’, as you have defined it, is simply a table that holds references to display objects.

you can’t insert a table into a scrollview, but you can insert each display object as you create them!

It would be really helpful if you posted your code using formatting. Next time, please click the blue <> button in the row with Bold, Italic, etc. and paste your code into the window that pops up.

You also said  you’re getting an error. It would be really good to know what that error is.

Rob

JWiow,

Your suggestion worked. Didn’t think of doing it that way. Thanks.

You haven’t posted any code so we can’t tell you what’s wrong with it.

As Lua doesn’t have arrays, I’m guessing you mean a table - so it’s possible you’re trying to add a table to a scrollview. If you read the error output, the console will tell you that cannot be done because a table is not a display object and only display objects can be added to scrollviews.

Again, if you post your code we will be able to help more.

Here is the code:


– main.lua


– Your code here

local composer = require “composer”

local scene = composer.newScene()

local widget = require “widget”

local height1 = display.contentHeight/6

local scrollView = widget.newScrollView

{

backgroundColor = {0.215, 0.67, 0.27},

left = 0,

top = 0,

width = display.contentWidth,

height = display.viewableContentHeight + 200,

topPadding = 30,

bottomPadding = 0,

horizontalScrollDisabled = false,

verticalScrollDisabled = false,

scrollHeight = display.contentHeight

}

scrollView.x = display.contentWidth/2

scrollView.y = display.contentHeight/2

–add new contact

local add = widget.newButton

{

left = 5,

top = 20,

width = 60,

height = 60,

id = “plus”,

label = “+”,

labelColor = {default = {1,1,1}, over = {1,1,1}},

fontSize = 140

}

add.x = display.contentWidth - add.width/2 - 10

add.y = add.height/4

function scene:create (event)

local homescreenGroup = self.view

–array for contacts

local contacts = {}

local numContacts = 1

local x = display.contentWidth/2

local y = -20

scrollView:insert(add)

–scrollView:insert(contacts)

homescreenGroup:insert(scrollView)

local function loadContacts()

y = y + display.contentHeight/6 + 30

contacts[numContacts] = display.newRect(x, y, display.contentWidth/1.5, display.contentHeight/6)

composer.gotoScene(“form”)

end

local function createContact()

numContacts = numContacts + 1

loadContacts()

end

add:addEventListener(“tap”, createContact)

end

function scene:hide(event)

end

function scene:show(event)

end

scene:addEventListener(“create”, scene)

scene:addEventListener(“show”, scene)

scene:addEventListener(“hide”, scene)

return scene

Is that really your main.lua? or is that just some misleading comments? you have a number of issues to sort out if so.

Assuming this is a scene file, and I’ve understood your requirement correctly, the only thing you appear to be missing is a line to add the rectangles into the scrollview in your loadContacts() function.

e.g. scrollView:insert(contacts[numContacts])

‘contacts’, as you have defined it, is simply a table that holds references to display objects.

you can’t insert a table into a scrollview, but you can insert each display object as you create them!

It would be really helpful if you posted your code using formatting. Next time, please click the blue <> button in the row with Bold, Italic, etc. and paste your code into the window that pops up.

You also said  you’re getting an error. It would be really good to know what that error is.

Rob

JWiow,

Your suggestion worked. Didn’t think of doing it that way. Thanks.