How do I create a UICollectionView with column and row headers?

后端 未结 2 452
遥遥无期
遥遥无期 2020-12-29 16:39

I want to create a UICollectionView that looks like this:

\"Yep.

It won\'t be scrollable or edi

2条回答
  •  春和景丽
    2020-12-29 17:10

    You can do it with UICollectionViewFlowLayout but if you go that way you need to count carefully and get the padding (i.e. the top left, and other "empty" cells) right. If you miscount it's obvious because the flow ruins everything quickly and you will find your header cells halfway down your columns. (I have done a similar thing)

    I only did it that way because of a fear of custom layouts - but in fact it is as easy as UITableView. If your layout does not scroll at all then yours will be particularly simple as your only real work will be calculating frame values for cells, to be returned in layoutAttributesForItemAtIndexPath. Since your whole view fits in the visible area, layoutAttributesForElementsInRect will mean you simply iterate through all the cells in the view. collectionViewContentSize will be the size of your view.

    Looking at your sample picture you might find it convenient to organise your data as a dictionary of arrays, one per column. You can get a column array by name ("A", "B" etc.) and the position in the array corresponds to the value in the leftmost column, which you might name "index".

    There are many more methods you can use but those are the basics, and will get your basic display up and running.

提交回复
热议问题