Use a Custom Type To Populate The New Grid Layout With Data
Daily Coding Tip 114
These custom types makes it possible to use
ForEach in the new
Grid layout, because each
GridRowContent and each
GridItemContent conform to
Identifiable. This makes them unique, even though the data I’m using is just strings that wouldn’t be
Identifiable on their own. To make the initialiser easier to use I’ve allowed an array of strings to be passed in, which are then mapped to a
GridItemContent object that holds their value and identifies them uniquely.
I’m going to use letters of the alphabet as my data, primarily because using this many full words makes the grid a bit crowded on small screens. The interface for this is very simple, I simply wanted to have a variable number of grid rows that wasn’t determined by the view body. I haven’t made
rows into a
Published property so it can’t be changed, but it’s easy to see how this could be used with data that changes at runtime.
Support Type Safely
You can help these tutorials succeed by doing one of these: