Help Needed with Collection View Grid - rewritten swift project

SamPollockSamPollock GBMember ✭✭

Hi All, Just wondered if anyone can help I am required to display some grid data in both an iOS app and Android app which I have written. I found the following blog post for the iOS option.

https://www.brightec.co.uk/ideas/uicollectionview-using-horizontal-and-vertical-scrolling-sticky-rows-and-columns

https://github.com/brightec/CustomCollectionViewLayout

which allows a static header and grid view as I need one object per row i.e (titles in the first row and then date, time, name etc etc for each row)

I also looked at the dev express control as an option however my app is not written as a forms app and have never used forms so I have no idea how to implement the control or whether it is suitable as it seems overkill. The grid also needs to be clickable per row to segue to another VC to display the detail.

public class TachRecordsFlowLayout : UICollectionViewLayout
    {
        int numberOfColumns = 4;
        bool shouldPinFirstColumn = true;
        bool shouldPinFirstRow = true;

        List<List<UICollectionViewLayoutAttributes>> itemAttributes = new List<List<UICollectionViewLayoutAttributes>>();

        List<CGSize> itemsSize = new List<CGSize>();
        CGSize contentSize = CGSize.Empty;


        public TachRecordsFlowLayout()
        {
        }

        public TachRecordsFlowLayout(IntPtr handle) : base(handle)
        {
        }

        public override void PrepareLayout()
        {
            if (this.CollectionView.NumberOfSections() == 0)
            {
                return;
            }

            if (itemAttributes.Count != CollectionView.NumberOfSections())
            {
                GenerateItemAttributes(CollectionView);
                return;
            }

            for (int section = 0; section < CollectionView.NumberOfSections(); section++)
            {
                for (int item = 0; item < CollectionView.NumberOfItemsInSection(section); item++) {
                    if (section != 0 && item != 0) {
                        continue;
                    }

                    var attributes = LayoutAttributesForItem(NSIndexPath.FromItemSection(item, section));
                    if (section==0) {
                        var frame = attributes.Frame;
                        frame.Y = CollectionView.ContentOffset.Y;
                        attributes.Frame = frame;
                    }
                }
            }

        }

        public override CGSize CollectionViewContentSize
        {
            get
            {
                return contentSize;
            }
        }

        public override UICollectionViewLayoutAttributes LayoutAttributesForItem(NSIndexPath indexPath)
        {
            return itemAttributes[indexPath.Section][indexPath.Row];
        }

        public override UICollectionViewLayoutAttributes[] LayoutAttributesForElementsInRect(CGRect rect)
        {

            var attributes = new List<UICollectionViewLayoutAttributes>();
            foreach (var section in itemAttributes)
            {
                var filteredArray = section.Where(obj => rect.IntersectsWith(obj.Frame));

                attributes.AddRange(filteredArray);
            }

            return attributes.ToArray();
        }



        public override bool ShouldInvalidateLayout(UICollectionViewLayoutAttributes preferredAttributes, UICollectionViewLayoutAttributes originalAttributes)
        {
            return true;
        }


        void GenerateItemAttributes(UICollectionView collectionView)
        {
            if (itemsSize.Count != numberOfColumns)
            {
                CalculateItemSizes();
            }

            int column = 0;
            nfloat xOffset = 0;
            nfloat yOffset = 0;
            nfloat contentWidth = 0;

            itemAttributes = new List<List<UICollectionViewLayoutAttributes>>();

            for (int section = 0; section < collectionView.NumberOfSections(); section++)
            {

                var sectionAttributes = new List<UICollectionViewLayoutAttributes>();

                for (var index = 0; index < numberOfColumns; index++)
                {

                    var itemSize = itemsSize[index];
                    var indexPath = NSIndexPath.FromItemSection(index, section);
                    var attributes = UICollectionViewLayoutAttributes.CreateForCell(indexPath);
                    attributes.Frame = new CGRect(xOffset, yOffset, itemSize.Width, itemSize.Height).Integral();

                    if (section == 0 && index == 0)
                    {
                        // First cell should be on top
                        attributes.ZIndex = 1024;
                    }
                    else if (section == 0 || index == 0)
                    {
                        // First row/column should be above other cells
                        attributes.ZIndex = 1023;
                    }

                    if (section == 0)
                    {
                        var frame = attributes.Frame;
                        frame.Y = collectionView.ContentOffset.Y;
                        attributes.Frame = frame;
                    }
                    if (index == 0)
                    {
                        var frame = attributes.Frame;
                        frame.X = collectionView.ContentOffset.X;
                        attributes.Frame = frame;
                    }

                    sectionAttributes.Add(attributes);
                    xOffset += itemSize.Width;
                    column += 1;

                    if (column == numberOfColumns)
                    {
                        if (xOffset > contentWidth)
                        {
                            contentWidth = xOffset;
                        }

                        column = 0;
                        xOffset = 0;
                        yOffset += itemSize.Height;
                    }
                }

                itemAttributes.Add(sectionAttributes);
            }

            var lastattributearray = itemAttributes[itemAttributes.Count - 1];
            var lastattributes = lastattributearray[lastattributearray.Count - 1];

            contentSize = new CGSize(contentWidth, lastattributes.Frame.GetMaxY());

        }

        void CalculateItemSizes()
        {
            itemsSize = new List<CGSize>();

            for (var index = 0; index < numberOfColumns; index++)
            {
                itemsSize.Add(SizeForItemWithColumnIndex(index));
            }       
        }

        CGSize SizeForItemWithColumnIndex(int columnIndex) {
            NSString text;

            switch (columnIndex) {
                case 0:
                    text = new NSString("MMM-99");
                    break;
                default:
                    text = new NSString("Content");
                    break;
            }

            var fsize = new nfloat(14.0);
            CGSize size = text.StringSize(UIFont.SystemFontOfSize(fsize));
            var width = size.Width + 16;
            return new CGSize(width, 30);
    }

    }
Sign In or Register to comment.