ScrollView Remove Elements?

I’m using a scrollView as an inventory of purchasable items.

For each item in my data structure I call addItem() which essentially creates an image, a bit of text and a button for each item and inserts this into a display group.

I then insert this display group into the scrollView when the player clicks a button to make it visible. I need the scrollView to house different categories of items, so I’m trying to remove the elements as needed but running into an error every time:

?:0: attempt to index field ‘parent’ (a nil value)
message
stack traceback:
?: in function <?:218>
[C]: ?
?: in function <?:459>
?: in function <?:218>

The code I’m using to remove the elements from the scrollView is:

[lua]

for i = self.struct[“itemScrollView”].numChildren,1,-1 do
            local child = self.struct[“itemScrollView”][i]
            child:removeSelf()
            child = nil
            --self.struct[“itemScrollView”]:remove(child)
            --self.struct[“itemScrollView”][i]:removeSelf()
        end

[/lua]

This runs through as expected (three times as I have 3 different items), however after running through this and ultimately coming to the next tick of my gameloop I’m presented with the aforementioned error.

As you can see from the commented lines I’ve tried numerous different approaches, but each causes the same error.

Any help would be appreciated.

I have no idea why the code formatting is like that - never happened to me before and I’ve tried to remove all whitespace and redo but no luck - apologies…

Looking at the code (on GitHub) for the ScrollView I noticed the enterFrame function has a reference to a .parent, which the error message states - it also seems to be spewing the error message at me every tick - indicating this could be the cause.

I presume I’m doing things the wrong way and this is trying to make a reference to self.parent that doesn’t exist?!?

For the record - I really can’t use the technique that has been suggested of just removing the ScrollView.

Hi @SegaBoy,

I’m probably grossly over-simplifying your scenario, but why can’t you just insert the new content into the actual ScrollView group (the widget itself), and then build some kind of table-based “indexing” system so you can later go back and reference those objects and remove them?

Brent

Hi Brent - I’ve tried your suggestion, putting the items directly into the scrollView. But still getting the “attempt to index field ‘parent’ (a nil value)” error.

I’m using the following syntax, which I presume is correct:

scrollView:insert(itemBtn)

scrollView:insert(itemImg)

scrollView:insert(itemTitle)

Then I’m attempting to remove them like so:

[lua]

for i = self.struct[“seedScrollView”].numChildren,1,-1 do
                local child = self.struct[“seedScrollView”][i]
                child:removeSelf()
                child = nil
                --self.struct[“itemScrollView”]:remove(child)
                --self.struct[“itemScrollView”][i]:removeSelf()
            end

[/lua]

I’ve tried to find documentation on the correct way to remove elements from a scrollView but can’t seem to find anything on the API pages; could you perhaps direct me to the correct place if you know these exist? The sample app doesn’t contain this functionality.

Cheers,

P.S A quick Google check on the error message revealed what I suspect to be a similar issue, however the solution of removing the scrollView and recreating it unfortunately isn’t valid for my design. I’m using a data-driven approach for UI and it would need a significant rewrite to facilitate this:

http://forums.coronalabs.com/topic/36032-0-attempt-to-index-field-parent-a-nil-value-message-stack-traceback/

@SegaBoy,

Removing items like that in a scrollview will 100% of the time cause that error, I am not sure why and not sure if it is G2.0 as I have never tried it with G1.0… So taking what Brent said and expanding it out a little bit for you, I wrote the code below and tested it works just fine using this method.

I am not sure how others use Corona but I for one always put resources in tables so when i leave the function/scene/page i am in i can simply loop through and remove them and or tag them with “id” so i can get access to anyone of them at anytime without having a bunch of declares and what not but that is just me.

local iconCache = {} local imageResource = display.newImage(scrollView, imageSheet, index) imageResource.anchorX, imageResource.anchorY = 0, 0 imageResource.x, imageResource.y = 0, 0 pcall(function() table.insert(iconCache, imageResource) end) scrollView:insert(imageResource) --[[--REMOVE ALL ITEMS FROM SCROLLVIEW for i = 1, #iconCache do pcall(function() iconCache[i]:removeSelf() end) pcall(function() iconCache[y] = nil end) end ]]--

You don’t need the pcalls etc. i just put them in so it will ignore errors if for some reason or other there is an issue with iconCache and it is 3am in the morning and i am a little brain dead.

Enjoy

I have no idea why the code formatting is like that - never happened to me before and I’ve tried to remove all whitespace and redo but no luck - apologies…

Looking at the code (on GitHub) for the ScrollView I noticed the enterFrame function has a reference to a .parent, which the error message states - it also seems to be spewing the error message at me every tick - indicating this could be the cause.

I presume I’m doing things the wrong way and this is trying to make a reference to self.parent that doesn’t exist?!?

For the record - I really can’t use the technique that has been suggested of just removing the ScrollView.

Hi @SegaBoy,

I’m probably grossly over-simplifying your scenario, but why can’t you just insert the new content into the actual ScrollView group (the widget itself), and then build some kind of table-based “indexing” system so you can later go back and reference those objects and remove them?

Brent

Hi Brent - I’ve tried your suggestion, putting the items directly into the scrollView. But still getting the “attempt to index field ‘parent’ (a nil value)” error.

I’m using the following syntax, which I presume is correct:

scrollView:insert(itemBtn)

scrollView:insert(itemImg)

scrollView:insert(itemTitle)

Then I’m attempting to remove them like so:

[lua]

for i = self.struct[“seedScrollView”].numChildren,1,-1 do
                local child = self.struct[“seedScrollView”][i]
                child:removeSelf()
                child = nil
                --self.struct[“itemScrollView”]:remove(child)
                --self.struct[“itemScrollView”][i]:removeSelf()
            end

[/lua]

I’ve tried to find documentation on the correct way to remove elements from a scrollView but can’t seem to find anything on the API pages; could you perhaps direct me to the correct place if you know these exist? The sample app doesn’t contain this functionality.

Cheers,

P.S A quick Google check on the error message revealed what I suspect to be a similar issue, however the solution of removing the scrollView and recreating it unfortunately isn’t valid for my design. I’m using a data-driven approach for UI and it would need a significant rewrite to facilitate this:

http://forums.coronalabs.com/topic/36032-0-attempt-to-index-field-parent-a-nil-value-message-stack-traceback/

@SegaBoy,

Removing items like that in a scrollview will 100% of the time cause that error, I am not sure why and not sure if it is G2.0 as I have never tried it with G1.0… So taking what Brent said and expanding it out a little bit for you, I wrote the code below and tested it works just fine using this method.

I am not sure how others use Corona but I for one always put resources in tables so when i leave the function/scene/page i am in i can simply loop through and remove them and or tag them with “id” so i can get access to anyone of them at anytime without having a bunch of declares and what not but that is just me.

local iconCache = {} local imageResource = display.newImage(scrollView, imageSheet, index) imageResource.anchorX, imageResource.anchorY = 0, 0 imageResource.x, imageResource.y = 0, 0 pcall(function() table.insert(iconCache, imageResource) end) scrollView:insert(imageResource) --[[--REMOVE ALL ITEMS FROM SCROLLVIEW for i = 1, #iconCache do pcall(function() iconCache[i]:removeSelf() end) pcall(function() iconCache[y] = nil end) end ]]--

You don’t need the pcalls etc. i just put them in so it will ignore errors if for some reason or other there is an issue with iconCache and it is 3am in the morning and i am a little brain dead.

Enjoy