is there a way to get the row height for each row in an UITableView
in swift ? Please help. Thanks in advance.
On the Home tab, in the Cells group, click Format. Under Cell Size, click AutoFit Row Height. Tip: To quickly autofit all rows on the worksheet, click the Select All button, and then double-click the boundary below one of the row headings.
To change the height of tableView cell in ios dynamically, i.e resizing the cell according to the content available, we'll need to make use of automatic dimension property. We'll see this with the help of an sample project.
add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.
Swift 4:
var height: CGFloat = 0
for cell in tableView.visibleCells {
height += cell.bounds.height
}
I think this is what you are looking for. This assumes that "Cell" is the identifier of the given row, and indexPath is the index of the row in question.
let row = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)as! UITableViewCell
let height = row.bounds.height
Cells only exist when they are visible, and you have access to them through the table view's visibleCells()
method.
for obj in tableView.visibleCells() {
if let cell = obj as? UITableViewCell {
let height = CGRectGetHeight( cell.bounds )
}
}
The functional way:
let sum = tableView.visibleCells.map( { $0.bounds.height } ).reduce(0,+)
print("Height:\(sum)")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With