Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get RowHeight of each row in UITableView Swift

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

like image 995
Shaik MD Ashiq Avatar asked Jul 01 '15 01:07

Shaik MD Ashiq


People also ask

How do you find the height of a cell?

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.

How do I change cell height in Swift?

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.

How do I get indexPath from cell Swift?

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.


4 Answers

Swift 4:

var height: CGFloat = 0
for cell in tableView.visibleCells {
    height += cell.bounds.height
}
like image 89
Alex Haas Avatar answered Oct 07 '22 22:10

Alex Haas


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
like image 28
Teddy Koker Avatar answered Oct 08 '22 00:10

Teddy Koker


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 )
    }
}
like image 33
Patrick Lynch Avatar answered Oct 07 '22 22:10

Patrick Lynch


The functional way:

 let sum = tableView.visibleCells.map( { $0.bounds.height } ).reduce(0,+)
 print("Height:\(sum)")
like image 27
Javier Calatrava Llavería Avatar answered Oct 07 '22 22:10

Javier Calatrava Llavería