What components make up a UITableView? - ios

I am trying to resize a grouped tableview 'tv', knowing that a new section (with a single row) is going to be added.
I thought that I could increase the height using:
newHeight = tv.frame.size.height + tv.rowHeight + tv.sectionFooterHeight + tv.sectionHeaderHeight;
but this seems to be too little (by about 30 pixels using default heights).
Am I missing some other part of the tableview? Maybe some buffer space between elements?
tv.sectionHeaderHeight and sectionFooterHeight both report a height of 10 - this seems fishy to me since the header looks at least twice the size of the footer.

Related

What size in cells is A4?

I'm trying to create a Google Sheets template that is exactly A4. The reason for that is I need to insert a picture in there that is absolutely placed and sized.
So, I'm figuring that since A4 is 210x297mm and Google Sheets default resolution is 96dpi, I need 793.59x1122.363 pixels.
Since a cell is 100x21 pixels, I need 7 cells horizontally + an additional cell that is 93 pixels large and 53 cells vertically + a cell that is 9 pixels high.
Which prints too big (A4, portrait, Scale Normal 100%, Margins 0,0,0,0):
too big
If I manually adjust the size of the cells to get a complete grey area without white lines on a single page, like this:
perfect size
I need 7 cells + one cell that is 85 px large x 53 cells + one cell that is 8 px high.
Note that (53*21+8)/785 = 1.428, which is not sqrt(2), the standard A4 ratio.
Why ? Thanks!

Custom UICollection view layout

I'm trying to configure a collection view to have the following custom layout shown in the attached image:
. Essentially, I want to be able to configure my collection view so it appears to be based off rows instead of columns. For example:
Row 1: 3 equally sized squares, each 1/3 the width of the screen
Row 2: 1 square that's 2/3 the width of the screen, and then 2 squares stacked on
top of each other that are each 1/3 the width of the screen.
Row 3: 1 rectangle that fills the width of the screen
Row 4: 2 equally sized squares, each 1/2 the width of the screen
I've gone through multiple UICollectionViewLayout and UICollectionViewDelegateFlowLayout tutorials, but have been unable to find the information I need.
You need to implement a custom UICollectionViewLayout.
Precompute the location of all of the squares in your repeat in prepareLayout
Return the height of your content by using the (repeat height) * (number of items in collection) / (number of items in repeat) + the height at the remainder in collectionViewContentSize (this means you should cache the content height for each item in step 1, but the height is not the height of that square (its the height of the tallest element in that row)
In layoutAttributesForElementsInRect you need to figure out what repeat row your rect starts in and what row of the repeat it ends in. You then return all of the cached layout attributes for those rows from step 1, but you have to add back a y value equal to (repeat count) / (first item index) * (repeat height).
basically its a bunch of math but when you divide it into repeating sections, the problem becomes which repeat am I in (divide) and what element within that repeat do I need (modulus), and it's much easier to solve.
I did something similar to this but with multiple repeats that could be arranged in different orders and it took a few hours to get it working without any bugs. Its a lot of math and very tedious.

Resize UICollectionViewCell to fit image

I'd like to resize UICollectionViewCell to fit image that is sensibly resized and maintains dimensions/aspect ratio. If I simply set the size of cell to size of image it may be way too big. Also, If I run on different sized devices, the spacing isn't consistent. Must I implement collectionView programmatically to overcome this?
I see many apps that do this so its a very common problem that may already be solved as a framework.
Example:
UICollectionView is actually of arbitrary layout. It so happens that the default is UICollectionViewFlowLayout which is a grid, but you can change it to anything by implmenting your own instance of the UICollectionViewLayout protocol. Int the case you cited its pretty easy. The width of the cells is the collectionview (width minus the padding) divided by two. The height is the aspect ratio times the width + the spacing + the label height. all the cells with indices divisible by 2 go in the right column and the rest go in the left column. It should be easy to calculate these values for layoutAttributesForItem(at:). Unfortunately its a bit harder to calculate for collectionViewContentSize. Assuming your collectionview is sufficiently small I think its best to just precalculate all of the hieghts and use the cached values for these functions.
There're a lot of open sources on github You can refer to.
https://github.com/zhangsuya/SYStickHeaderWaterFall

Determine UILabel height for centering when using adjustFontSizeToFitWidth

I have the following cell design where the numeric label shrinks and the "Overall" label is directly underneath.
I have properly set the adjustFontSizeToFitWidth and minimumFontSize properties. The font is resizing correctly. However, anchoring the numeric label to the bottom is challenging. Particularly when the font shrinks the gap between the two labels widens and does not appear vertically centered.
I have tried sizeToFit, sizeThatFits, and using the font's pointSize. All unsuccessfully.
I am aware of sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:, but don't understand why I would need it in combination with adjustFontSizeToFitWidth.
Ah, so you want to position the UILabels in the middle of the container view (both horizontally and vertically)?
I have rephrased my answer so it will make more sense to future readers.
My code is assuming that you have the 3 IBOutlets set up:
UIView *containerView; //Your nice view containing the two textfields.
UILabel *points; //The label containing the number.
UILabel *overall; //The textfield containing the text 'overall'.
You could simply set the frame of the labels after assigning the text and calling the sizeToFit.
This first line positions the UILabel points, the only change being that the y coordinate is half of containerView subtract half of the height of itself.
points.frame = CGRectMake(points.frame.origin.x, (containerView.frame.size.height / 2) - (points.frame.size.height / 2), points.frame.size.width, points.frame.size.height);
To position the overall accordingly - say there is a distance of say 6 between the number and overall labels:
int space = 6;
overall.frame = CGRectMake(overall.frame.origin.x, points.frame.origin.y + points.frame.size.height + space, overall.frame.size.width, overall.frame.size.height);
Having read your comments, I think you are after this solution. If you want both UILabels to appear in the middle; subtract (overall.frame.size.height / 2) + (space / 2) from the y value of points like so (with the code of number 2 beneath it):
int space = 6;
points.frame = CGRectMake(points.frame.origin.x, ((containerView.frame.size.height / 2) - (points.frame.size.height / 2)) - ((overall.frame.size.height / 2) + (space / 2)), points.frame.size.width, points.frame.size.height);
overall.frame = CGRectMake(overall.frame.origin.x, points.frame.origin.y + points.frame.size.height + space, overall.frame.size.width, overall.frame.size.height);
The final point will produce an output like this image. As you can see the blue line is half of the whole image, and intersects the black rectangle (which is snuggly around the two labels) at its middle point. I hope this is what you were after.
Instead of using two labels, use CATextLayer instead. You will be easily able to make one part BOLD and the other normal. plus position and adjusting size for One layer will be easy relative to placing two labels. shadow setting, line break mode, fonts you will be able to adjust everything beautifully :)

How high is the spacing between UITableViewCell?

Anybody know how big is the spacing between UITableViewCells in points ? That is I would like to know whether there are any screen spaces allocated between cells in an UITableView and how high are they, in each of these separator styles:
UITableViewCellSeparatorStyleNone – is it 0pt ?
UITableViewCellSeparatorStyleSingleLine – is it 1pt?
UITableViewCellSeparatorStyleSingleLineEtched – is it 1pt or 2pt?
The problem I'm currently facing is that I have a variable-height cells and I need to calculate how high a group of cells may be. Currently I'm using UITableViewCellSeparatorStyleSingleLine separator style and assuming that the inter-cell spacing is 1pt high but my calculations are often off.
That is, supposed that there are three cells, each 40 pt high, my assumption is that the total height will be 40 + 1 + 40 + 1 + 40 = 122 points high.
Questions are:
Is it true that a single line separator takes 1pt height between the cells?
If not, what is the height of the separator line, if any space is allocated at all (i.e. doesn't "eat" into the adjacent cell's space.
Thanks in advance.
This is a problem you can solve very easily by taking a screenshot from interface builder and then measuring it in Photoshop, however I've saved you the trouble:
The separator is not added to the the height of the table cell - it is drawn in front of the cell, so if your table cell height is 44 (the default) then the combined height of two cells will be 88, regardless of which separator style you choose.

Resources