Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: spyridon on June 24, 2014, 01:27:20 PM

Title: Adding contents to multiple tables in order?
Post by: spyridon on June 24, 2014, 01:27:20 PM
Basically I have a set of 3 tables, first table is labels, other 2 are textboxes, they are added to dynamically during runtime with different numbers of objects. You can think of it working basically of like a spreadsheet.

For example if my data contents are the following:

Item01 - 1 - 1.11
Item02 - 2 - 2.22
Item03 - 3 - 3.33
Item04 - 4 - 4.44

When I add those to the table, I have a loop to add to my interface using the following code:

  1.             // load label object
  2.             var newItem = Instantiate(productLabelObject, this.transform.position, this.transform.rotation) as GameObject;
  3.             newItem.transform.parent = productCodeList.transform;
  4.             newItem.transform.localScale = Vector3.one;
  5.  
  6.             // load quantity object
  7.             var newItem2 = Instantiate(productTextObject, this.transform.position, this.transform.rotation) as GameObject;
  8.             newItem2.transform.parent = quantityList.transform;
  9.             newItem2.transform.localScale = Vector3.one;
  10.  
  11.             // load price object
  12.             var newItem3 = Instantiate(productTextObject, this.transform.position, this.transform.rotation) as GameObject;
  13.             newItem3.transform.parent = priceList.transform;
  14.             newItem3.transform.localScale = Vector3.one;
  15.  
  16.             // set text of label object
  17.             UILabel labelScript = newItem.GetComponent<UILabel>();
  18.             labelScript.text =  orderArray[0, yT];
  19.  
  20.             // set text of quantity object ([2])
  21.             UIInput inputScript = newItem2.GetComponent<UIInput>();
  22.             inputScript.value = orderArray[2, yT];
  23.  
  24.             // set text of price object ([2])
  25.             UIInput inputScript2 = newItem3.GetComponent<UIInput>();
  26.             inputScript2.value = orderArray[1, yT];
  27.  

Then I call Reposition() for each table at the end.

Even though I added each object to the table in the same exact order, for some reason the textboxes are all mixed up in the order they display. This is the current display I get for the data I listed above:

Item01 - 2 - 2.22
Item02 - 4 - 4.44
Item03 - 3 - 3.33
Item04 - 1 - 1.11

I have no idea why this is happening like this? Sort is unchecked, so I'm not sure how to get them ordered in the appropriate order. Is there any way I could get them always listed in the order that I add them to the table?

Thanks
Title: Re: Adding contents to multiple tables in order?
Post by: ArenMook on June 25, 2014, 06:06:20 AM
1. Never use Instantiate. Use NGUITools.AddChild. It will set the parent, scale, and layer for you.

2. Have you considered creating a prefab out of a single row instead, and then simply instantiating this prefab, positioning it (or using UIGrid to reposition it for you)? It would be an order of magnitude more performant than using tables.

3. Adding objects doesn't mean that their order will be kept. Unity is funny like that. If you want to guarantee sorting order, enable sorting and name objects sequentially using alphabetic characters, like "001", "002", "003", etc. Note that the sorting is not alphanumeric. It's alphabetic.