Correct way to determine the width of a UITableViewCell for grouped user cells?

I have a custom UITableViewCell created using code (and not Interface Builder). I have subviews that rely on cell width: there is a label that should always only be inside the right edge of the cell. Cells are displayed in grouped tables.

I initially hardcoded the values ​​for working with the iPhone, but now I convert the application to universal binary code, and hardcoded numbers are displayed for grouped table views on the iPad.

Is there any way to get cell width? The frame returns the entire width of the screen, which throws my shortcuts into place. Or do I just need to copy two sets of values, one for the iPhone and one for the iPad?

Any suggestions are welcome.

+4
source share
2 answers

You should be able to get the appropriate sizes from the contentView subview cell. But usually you should just set the sizes and autoresist the masks on your subzones so that the cell displays correctly, regardless of which width ends.

+9
source

None of this seemed useful to me; I have a custom cell defined in xib with graphics against the left edge (20 pixels on the iPhone), looks great on the iPhone. But on the iPad, the graphics overlapped the left grouped section border! To fix this, I set the output to limit the horizontal space (leading) to the image, and then changed it in the code for the UITableViewCell subclass:

 - (void)setFrame:(CGRect)frame { if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { self.leadingSpaceConstraint.constant = 40; } [super setFrame:frame]; } 

And I tried the number until the gap was right. :)

I understand that setFrame: may not have been the perfect place for this code, but it worked, so it stayed.

0
source

Source: https://habr.com/ru/post/1342747/


All Articles