How to position object so they don't overlap?

I have a bunch of content that I have added to a scrollView with the help of a for loop, the problem is that the objects overlap because they are of different height.

How do I position all objects so they don’t overlap anymore?

 for i = 1, #data do  
 local contentBox = display.newGroup()  
   
 local messageText = display.newText(contentBox, data[i].message, 0, 0, 280, 0, "Arial", 14)  
 messageText:setReferencePoint(display.TopLeftReferencePoint)  
 messageText.x = 10;  
 messageText.y = 10;  
 messageText:setTextColor(255)   
  
 local contentBoxLine = display.newRect(contentBox, 0, 0, 300, 1);  
 contentBoxLine:setReferencePoint(display.TopLeftReferencePoint);  
 contentBoxLine:setFillColor(164, 152, 205, 255);  
 contentBoxLine.x = 0;  
 contentBoxLine.y = messageText.y + messageText.height + 20;  
  
 -- Position the contentBox  
 contentBox:setReferencePoint(display.TopLeftReferencePoint);  
 contentBox.x = 10;  
 contentBox.y = (i-1) \* contentBox.height + 40  
  
 print(" --\> "..contentBox.height)  
   
 -- insert the boxes to scrollView.  
 myScrollView:insert(contentBox)  
 end  

/thanks
Cin [import]uid: 65840 topic_id: 28934 reply_id: 328934[/import]

You need to capture each contentBox group into an array. The way you have it coded you write over each contentBox for each loop iteration and you would have to find a way to reference each one inside the scrollview. It’s much easier to keep your own references…

Outside your for loop:

contentBox = {} -- make it a table/array  

Then inside your loop change each reference to codeBox to codeBox[i]

Then before you do the myScrollView:insert(contentBox) do this:

if i == 1 then  
 codeBox[i].y = 0  
else  
 codeBox[i].y = codeBox[i-1].y + codeBox[i-1].height  
end  

Or something like that. [import]uid: 19626 topic_id: 28934 reply_id: 116515[/import]

@Rob

duh…should have figured that one out myself cuz I did it similar to that in another place…
Thanks Rob.
/C.
[import]uid: 65840 topic_id: 28934 reply_id: 116588[/import]