Author Topic: UITable Reposition not working correctly?  (Read 3668 times)

AssyriaGameStudio

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
UITable Reposition not working correctly?
« on: June 13, 2015, 01:28:04 PM »
Hi,
So basically I have a scrollview, utilising a UITable.

Within this table I have some items, some of which are dynamically updated once I get a list of items from the server.

I have the following code which positions the items as children of an item in the table, and then expands the table item (the background of the list) to match the height... I then call reposition on the table.

  1.                 foreach(GameObject obj in spawned_catagory_items)
  2.                 {
  3.                         NGUITools.Destroy(obj);
  4.                 }
  5.                 spawned_catagory_items.Clear();
  6.                 create_event_table_page1.Reposition();
  7.                 bool left_side = true;
  8.                 int position = 0;
  9.                 for(int i=1; i< UserSettings.catagories.Count; i++)
  10.                 {
  11.                         if(left_side)
  12.                         {
  13.                                 left_side = false;
  14.                                 GameObject cat_item = NGUITools.AddChild(catagory_segment, catagory_toggle_prefab_left);
  15.                                 NGUITools.SetActive     (cat_item,true);       
  16.                                 cat_item.GetComponent<CatagoryToggler>().UpdateCat(UserSettings.catagories[i]);
  17.                                 spawned_catagory_items.Add(cat_item);
  18.                                 cat_item.transform.localPosition = new Vector2(0,-85 - (position * 70));
  19.                         }
  20.                         else
  21.                         {
  22.                                 left_side = true;
  23.                                 GameObject cat_item = NGUITools.AddChild(catagory_segment, catagory_toggle_prefab_right);
  24.                                 NGUITools.SetActive     (cat_item,true);       
  25.                                 cat_item.GetComponent<CatagoryToggler>().UpdateCat(UserSettings.catagories[i]);
  26.                                 spawned_catagory_items.Add(cat_item);
  27.                                 cat_item.transform.localPosition = new Vector2(0,-85 - (position * 70));
  28.                                 position++;
  29.                         }
  30.                 }
  31.  
  32.                 catagory_segment_sprite.height = 85 + (position * 70);
  33.                 create_event_table_page1.Reposition();
  34.  

The issue being that it's not actually repositioning the items and I end up with items overlaying each other.

Attached are screenshots of what I'm getting as a result, and a screenshots of the intended result (repositioned in the editor).

I guess the method I'm using to add items doesn't work with NGUI, but it would be great to get some indication as to how I should go about doing this.

Many thanks.




Razhagal

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 14
    • View Profile
Re: UITable Reposition not working correctly?
« Reply #1 on: June 13, 2015, 02:32:06 PM »
If "spawned_catagory_items" are actually items in that table before inserting the new ones you need to reparent them first before deleting them. The problems is that the destroying and creating new ones happen in the same frame. First you need to reparent them (transform.parent = null) and then destroy them. That way you can be sure the table will be empty when you start adding stuff in it.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITable Reposition not working correctly?
« Reply #2 on: June 16, 2015, 12:33:50 PM »
NGUITools.Destroy takes care of setting the parent to null.

Keep in mind destroying objects in a list while iterating through that list is a bad idea. NGUITools.DestroyChildren can safely remove all children of a game object for you.