コード例 #1
0
ファイル: PUTable.cs プロジェクト: ly774508966/PlanetUnity2
    public PUTableCell LoadCellForData(object cellData, PUTableCell reusedCell, float currentContentHeight)
    {
        PUTableCell cell = reusedCell;

        if (reusedCell == null)
        {
            string className = cellData.GetType().Name + "TableCell";
            Type   cellType  = Type.GetType(className, true);

            cell = (Activator.CreateInstance(cellType)) as PUTableCell;
            cell.LoadIntoPUGameObject(this, cellData);
        }
        else
        {
            cell.cellData            = cellData;
            cell.puGameObject.parent = this;
            cell.puGameObject.rectTransform.SetParent(this.contentObject.transform, false);

            cell.UpdateContents();

            if (cell.IsHeader() == false)
            {
                //cell.animatedYOffset = cell.puGameObject.rectTransform.anchoredPosition.y - currentContentHeight;
            }
        }

        allCells.Add(cell);

        cell.puGameObject.rectTransform.anchoredPosition = new Vector3(0, currentContentHeight, 0);
        cell.puGameObject.rectTransform.anchorMin        = new Vector2(0, 1);
        cell.puGameObject.rectTransform.anchorMax        = new Vector2(0, 1);
        cell.puGameObject.rectTransform.pivot            = new Vector2(0, 1);

        return(cell);
    }
コード例 #2
0
ファイル: PUTable.cs プロジェクト: magu-test/PlanetUnity2
    public void ReloadTable(bool reuseCells = true)
    {
        RectTransform contentRectTransform = contentObject.transform as RectTransform;


        if (gameObject.GetComponent <PUTableUpdateScript>() == null)
        {
            PUTableUpdateScript script = (PUTableUpdateScript)gameObject.AddComponent(typeof(PUTableUpdateScript));
            script.table = this;
        }

        // This one will set the content size of the scrolling axis
        CalculateContentSize();


        // -2) Calculate the old height, so we can subtract it from the new height and adjust the scrolling appropriately
        float oldHeight = contentRectTransform.rect.height;


        // -1) Save previous cells for reuse if we can...
        List <PUTableCell> savedCells = new List <PUTableCell>(allCells);

        allCells.Clear();

        if (allObjects == null || allObjects.Count == 0)
        {
            return;
        }

        // 0) Remove all of the cells from the list transform temporarily
        foreach (PUTableCell cell in savedCells)
        {
            cell.puGameObject.rectTransform.SetParent(null, false);
        }

        // 1) Run through allObjects; instantiate a cell object based on said object class
        float currentContentHeight = 0;

        for (int i = 0; i < allObjects.Count; i++)
        {
            object myCellData = allObjects [i];

            // Can we reuse an existing cell?
            PUTableCell savedCell = null;
            if (reuseCells)
            {
                foreach (PUTableCell otherCell in savedCells)
                {
                    if (otherCell.IsHeader() == false && otherCell.cellData.Equals(myCellData))
                    {
                        savedCell = otherCell;
                        savedCells.Remove(otherCell);
                        break;
                    }
                }
            }

            PUTableCell newCell = LoadCellForData(myCellData, savedCell, currentContentHeight);

            currentContentHeight += newCell.puGameObject.rectTransform.rect.height;
        }

        for (int i = 0; i < allObjects.Count; i++)
        {
            PUTableCell cell = allCells [i];
            if (cell.IsHeader())
            {
                // TODO: Move me to the end of the stuff
                cell.puGameObject.parent = this;
                cell.puGameObject.gameObject.transform.SetParent(cell.puGameObject.gameObject.transform.parent, false);

                PUTableHeaderScript headerScript = cell.puGameObject.rectTransform.GetComponent <PUTableHeaderScript> ();
                headerScript.Start();
            }
        }

        // This one will set the content size of the scrolling axis
        CalculateContentSize();

        // This will layout our cells
        LateUpdate();

        // This will get the actual content size after the cells are successfully laid out
        CalculateContentSize();

        // 3) offset the scroll based upon the change in table height
        float newHeight = contentRectTransform.rect.height;

        if (oldHeight > 1)
        {
            Vector2 scroll = contentRectTransform.anchoredPosition;
            scroll.y += newHeight - oldHeight;
            contentRectTransform.anchoredPosition = scroll;
        }

        // 2) Remove all previous content which have not reused
        foreach (PUTableCell cell in savedCells)
        {
            cell.unload();
        }
    }