Author Topic: Adding item to UIGrid on index 0  (Read 5728 times)

Tripwire

  • Full Member
  • ***
  • Thank You
  • -Given: 9
  • -Receive: 0
  • Posts: 163
    • View Profile
Adding item to UIGrid on index 0
« on: July 07, 2014, 08:24:42 AM »
Hi,

What is the best way to add a child object in a scrollview's grid to index 0. I want my new items added to the top of the scrollview, and not at the bottom.

Thx!

Edit:
I'm using this code to add the items to the grid:
  1. public void AddItem(int smiley, int painvalue, int catagory, string notes, string exercise)
  2.         {
  3.                 GameObject clone = NGUITools.AddChild(Grid.gameObject, TimeLinePrefab);
  4.  
  5.                 UISprite[] sprites = clone.GetComponentsInChildren<UISprite>();
  6.  
  7.                 /*
  8.                  * sprites[0] = excercise sprite
  9.                  * sprites[2] = smiley
  10.                  */
  11.                 sprites[0].spriteName = exercise;
  12.                 SetSmile(smiley, sprites[2]);
  13.  
  14.                 UILabel[] labels = clone.GetComponentsInChildren<UILabel>();
  15.  
  16.                 if(labels != null)
  17.                 {
  18.                         /*
  19.                  * labels[0] = MiddleText
  20.                  * labels[1] = MiddleHeader
  21.                  * labels[2] = BottomText
  22.                  * labels[3] = Exercise_Body
  23.                  * labels[4] = Exercise_Header
  24.                  * labels[5] = PainValue
  25.                  */
  26.                        
  27.                         labels[0].text = "temp text";
  28.                         labels[1].text = now.ToString("dd:MM:yyyy");
  29.                         labels[2].text = notes;
  30.                         labels[3].text = exercise;
  31.                         SetCatagory(catagory, labels[4]);
  32.                         labels[5].text = painvalue.ToString() + "%";
  33.                 }
  34.  
  35.                 itemCounter++;
  36.                 clone.name = itemCounter.ToString();
  37.  
  38.                 currentItem = new Timeline();
  39.                 currentItem.Smile = smiley;
  40.                 currentItem.Pain = painvalue;
  41.                 currentItem.Notes = notes;
  42.                 currentItem.Exercise = exercise;
  43.                 currentItem.Date = now.ToString("dd:MM:yyyy");
  44.                 DM.CreateNewTimeLineItem(currentItem);
  45.  
  46.                 Grid.Reposition();
  47.                
  48.         }
  49.  
« Last Edit: July 07, 2014, 08:35:56 AM by Tripwire »

grofie

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 1
  • Posts: 17
    • View Profile
Re: Adding item to UIGrid on index 0
« Reply #1 on: July 08, 2014, 03:22:46 AM »
Make sure you set up the right Content Origin in your UIScrollView. Setting the origin to Top should order your list like you want it, having the first item at the very top.

Tripwire

  • Full Member
  • ***
  • Thank You
  • -Given: 9
  • -Receive: 0
  • Posts: 163
    • View Profile
Re: Adding item to UIGrid on index 0
« Reply #2 on: July 08, 2014, 07:46:24 AM »
Make sure you set up the right Content Origin in your UIScrollView. Setting the origin to Top should order your list like you want it, having the first item at the very top.

Hi grofie,

Thanks for your reply! I didn't think about that, I've had my origin set to top anyways, but maybe if I set it to bottom it will work :)

EDIT:
Tested it, doesn't work. Items are still added to the bottom of the scrollview instead of the top of the view.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Adding item to UIGrid on index 0
« Reply #3 on: July 08, 2014, 12:25:06 PM »
Name your child objects alphanumerically, such as "001", "002", etc, and enable alphabetic sorting on the UIGrid.

grofie

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 1
  • Posts: 17
    • View Profile
Re: Adding item to UIGrid on index 0
« Reply #4 on: July 09, 2014, 07:57:43 AM »
Sorry Tripwire,

I misread your post first time. You can simply add

  1. clone.transform.SetAsFirstSibling();

somewhere in your code after adding and before Reposition(). Adding a transform (also over NGUITools.AddChild) will always add it at last position. With this code it will then be put on the first place (Top), you won't need extra sorting then.

Just tried it out with my own scrollview, this time it should be really what you wanted ;)