I would like to create a grid of boxes, which are Label
's with fixed width and height within my UITableViewCell
's. How can this be done?
I want this:
I get this:
I tried using a UICollectionView
within the UITableViewCell
but I am running in to two problems:
- the
UITableViewCell
's height is not dynamically changing according to how many items there are - the space between items can't be set exactly (since you can only define a minCellSpacing value)
My Question:
What is the best way to create this grid of Label
's such that the UITableViewCell
's height will change dynamically based on number of items, and so I can set the spacing between items exactly?
(If it can be done without UICollectionView
that would be nice, but it's not a requirement)
Here's an updated version of the
initWithStyle:reuseIdentifier:
method that ensures that the labels are 30x30:It uses the default spacing of 8 pixels, but you can change that via the
spacing
property ofLMColumnView
andLMRowView
.If you are targeting iOS 9, you could do this by adding a collection of
UIStackView
s to your cell. The outer stack view would be vertical, and each row would be a horizontal stack view. You could useFillEqually
for the distribution to ensure that all of the cells are the same width.Alternatively, you could consider using
LMColumnView
andLMRowView
from the MarkupKit framework, which supports iOS 8. Here's an example:The source code for the view controller looks like this:
The cell code looks like this:
Basically, it generates a random number to represent the number of labels in each cell, then creates as many rows and labels as it needs to populate the cell. Empty labels are created to ensure that everything aligns.
You can add Uicollection View in Uitableview Cell itself .....else you will need to calculate the height of your Uitableview cell for no.of boxes you want to add in each cell...But i think collectionview is better way to achieve this can i know why you dont want to use uicollection view...