Get RowHeight of each row in a UITableView Swift

Is there a way to get the row height for each row in a UITableView in swift? Please help. Thanks in advance.

+11
source share
6 answers

Swift 4:

 var height: CGFloat = 0 for cell in tableView.visibleCells { height += cell.bounds.height } 
+9
source

I think this is what you are looking for. This assumes that the "Cell" is the identifier of the given row, and indexPath is the index of the corresponding row.

 let row = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)as! UITableViewCell let height = row.bounds.height 
+7
source

Cells exist only when they are visible, and you have access to them through the table view method visibleCells() .

 for obj in tableView.visibleCells() { if let cell = obj as? UITableViewCell { let height = CGRectGetHeight( cell.bounds ) } } 
+3
source

You need a cell for a specific IndexPath to calculate its borders.

You can do it this way in any of the functions of the UITableView delegate:

 let row = tableView.cellForRow(at: indexPath) let cellHeight = (row?.bounds.height)! let cellWidth = (row?.bounds.width)! 
0
source

Functional way:

  let sum = tableView.visibleCells.map( { $0.bounds.height } ).reduce(0,+) print("Height:\(sum)") 
0
source

If your rows are of uniform height, the rowHeight property should get what you want.

Otherwise, UITableViewDataSource expands numberOfRowsInSection and cellForRowAtIndexPath, while UITableViewCell has the rowHeight property.

If you are just trying to get the height of the view by adding the height of the rows, the contentSize property in the UITableView would be better for you.

-1
source

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


All Articles