示例#1
0
    public iCollectionViewCell CreateCell(string nib, GameObject owner = null)
    {
        //Check we have nib registered
        if (!m_nibs.ContainsKey(nib))
        {
            throw new UnityException(string.Format("No Prefab registered for {0}", nib));
        }

        //Check pool
        if (m_pools.ContainsKey(nib))
        {
            List <iCollectionViewCell> pool = m_pools[nib];

            //Check for any not being used
            foreach (iCollectionViewCell cell in pool)
            {
                if (!cell.IsActive)
                {
                    cell.SetActive(true, owner);
                    return(cell);
                }
            }
        }
        else
        {
            //Make Pool
            m_pools.Add(nib, new List <iCollectionViewCell>());
        }

        //No free cells, so make a new one
        iCollectionViewCell c = m_nibs[nib].CreateCell();

        c.SetActive(true, owner);

        m_pools[nib].Add(c);

        return(c);
    }
示例#2
0
    /// <summary>
    /// This handles reuse, it will work out the indices we need, add delete/add cell
    /// as needed.
    ///
    /// Call before layout
    /// </summary>
    /// <param name="data">Data - The whole data we are laying out.</param>
    /// <param name="cells">Cells - The array of cells we will add/delete from as needed</param>
    private void PrepareCells(List <object> data, List <iCollectionViewCell> cells)
    {
        int totCells = Math.Min(TotalCellsNeeded, data.Count);

        int curIndex = (int)Math.Round(CurrentIndex);
        int min      = curIndex - (totCells / 2);
        int max      = curIndex + (totCells / 2);

        if (!IndexWrap)
        {
            if (min < 0)
            {
                min = 0;
                max = totCells;
            }
            else if (max >= data.Count)
            {
                max = data.Count;
                min = max - totCells;
            }
        }

        //GO through cells, any with an index we no longer need, turn off
        int minVal = (min < 0) ? min + data.Count : min;
        int maxVal = (max >= data.Count) ? max - data.Count : max;


        if (!IndexWrap)
        {
            if (max >= data.Count)
            {
                maxVal = data.Count - 1;
                minVal = maxVal - m_maxCells;
            }

            min = minVal;
            max = maxVal;
        }


        for (int i = 0; i < cells.Count; i++)
        {
            iCollectionViewCell c = cells[i];
            c.Snap = false;

            //Bizzare annoyance,min and max can sometimes be the same, i.e. we have enough cells for all the data fine
            if (maxVal == minVal)
            {
                continue;
            }

            //If maxval is bigger than minval then just check were in the range
            if (maxVal > minVal && (c.Index <= maxVal && c.Index >= minVal))
            {
                continue;
            }

            //If min value is bigger than max were in a "wrap"
            if (minVal > maxVal && (c.Index >= minVal || c.Index <= maxVal))
            {
                continue;
            }
            c.SetActive(false);
            cells.Remove(c);
            i--;
        }

        //Go through cells, add any indices we need and set their data TODO - How to do this quickly?
        for (int i = min; i <= max; i++)
        {
            int index = i;
            //Wrap index
            if (index < 0)
            {
                index += data.Count;
            }
            else if (index >= data.Count)
            {
                index -= data.Count;
            }

            //Check if we have htis index
            bool found = false;
            foreach (iCollectionViewCell cell in cells)
            {
                if (cell.Index == index)
                {
                    found = true;
                    //Set Data if null
                    if (cell.Data == null)
                    {
                        cell.SetData(data[index]);
                    }
                    break;
                }
            }

            if (!found)
            {
                //Add a new one
                iCollectionViewCell c = CellPoolService.Instance.CreateCell(NibInUse, gameObject);
                c.transform.localPosition = Vector3.zero;
                c.Index = index;
                c.Snap  = true;
                c.SetData(data[index]);

                if (OnCellClick != null)
                {
                    c.OnClickEvent += OnCellClick;
                }

                cells.Add(c);
            }
        }
    }