1

I need to calculate the cell height of the table and round it to a divisible value by 5. For example, when the cell size is 116, the correct height should be 120.

The point is that I use the adaptive layout to calculate the cell height. One of the subviews of UITableViewCell is a vertical dashed line which should connect to cell below. The dashed line consists of repeating of 5 points - dash (2 points) and gap (3 points). So the height of line view should be rounded up to the value divisible by 5 for correct connection with dashed line of cell which is below.

Two cells with connected dashed line

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
zyxman
  • 72
  • 5

1 Answers1

0

Assuming that you only want to round up if the value is NOT already a multiple of 5 then something like this is useful: We need to specifically round down CGFloat (which is what UIViews measure in):

let currentHeight = CGFloat(123.456)
let newHeight = ((currentHeight + 4.99) / 5).rounded(.down) * 5
=> 125

let currentHeight = CGFloat(125)
let newHeight = ((currentHeight + 4.99) / 5).rounded(.down) * 5
=> 125

I would set the height of the cell to a multiple of five and the height of the dashed-line view to the same. (I think that is what you intended, just making sure).

Dont forget that you need to set the height of the table cell and the height of the tableCell.contentView. Plus you will need to let the UITableView know the height of the cell for its layout.

DrPhill
  • 614
  • 5
  • 16