Author Topic: UITable/UIScrollView Adjust position  (Read 6679 times)

EToreo

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 88
    • View Profile
UITable/UIScrollView Adjust position
« on: November 26, 2013, 06:36:55 PM »
I have a table and a scroll view that allows the player to vertically scroll though the cells in the table.  The player can swap out the entire content of that table by pressing a button.

When the content is swapped out, it is not "vertically aligned" to the top of the table - it starts somewhere near the center (it seems to have something to do with the amount of content that was there before and it's position before the swap happens).

Here is the code I am using to do this, what can I do to get the content to have the first cell aligned to the top of the visible table?
(The "rand" is a contrived example of the content being dynamic in size)
  1.  
  2.    for(int i = 0; i < Table.transform.childCount; i++)
  3.       Destroy(Table.transform.GetChild(i).gameObject);
  4.      
  5.    for(int i = 0; i < rand(5, 10); i++)
  6.    {
  7.       GameObject pNewGo = NGUITools.AddChild(Table.gameObject, Resources.Load("Prefabs/MainMenu/OptionsCellPrefab") as GameObject);
  8.       pNewGo.name = val;
  9.       pNewGo.transform.position = Vector3.zero;
  10.       pNewGo.transform.localScale = Vector3.one;
  11.       pNewGo.transform.localPosition = Vector3.zero;
  12.    }
  13.    Table.Reposition();
  14.  
  15.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITable/UIScrollView Adjust position
« Reply #1 on: November 26, 2013, 11:42:16 PM »
Are you ever resetting the scroll view's position? You need to do it by calling UIScrollView.ResetPosition, ideally before and after you re-populate your table.

macy

  • TNP Alpha
  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 2
  • Posts: 8
    • View Profile
Re: UITable/UIScrollView Adjust position
« Reply #2 on: November 26, 2013, 11:53:43 PM »
Table.Reposition();
Reposition all the items in the UITable.

If you call Table.Reposition() at the Start, all the items starts somewhere near the centre, then it means your UITable placed at that position and everything will be placed there. Please make sure the UITable is placed at the Top Left at first.

If that doesn't work, you can call the UIDraggablePanel.repositionClipping to reClipping the clipping range.
or

call the scrollbar, to set the value to 0.

plus Destroy doesn't work right the way, please try DestroyImmediate. If you do a count at your script, you may get more items in the Table than it looks.

EToreo

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 88
    • View Profile
Re: UITable/UIScrollView Adjust position
« Reply #3 on: November 27, 2013, 12:55:13 AM »
UIScrollView.ResetPosition() alone doesn't appear to do the trick... in all three cobinations of before and/or after.

As for Marcy's suggestions DestroyImmediate has unintended consequences and based on the documentation that makes sense.

EToreo

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 88
    • View Profile
Re: UITable/UIScrollView Adjust position
« Reply #4 on: December 06, 2013, 04:09:57 AM »
On the 3.0.7 notes you say:
Quote
- NEW: You can now create resizable scroll views and anchor them to UI elements.

Might this fix my problem?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITable/UIScrollView Adjust position
« Reply #5 on: December 06, 2013, 04:11:36 AM »
The new layout system fixes quite a few problems, so likely.