1

Is there any way by which I can get row height of each row in a UITableView. I have a UITableView which contains data which is dynamic, and I am using

self.tableView.rowHeight = UITableViewAutomaticDimension

to make dynamic row heights.

But I want to change height of overall UITableView as well, for that I need height of all the rows after reload of data.

I tried using

self.tableView.layoutIfNeeded()
self.tableViewHeightContraint.constant = self.tableView.contentSize.height

But due to unknown reason it is giving me height less than what exactly it has, after I add 3 or 4 records in UITableView so thinking of other way around. To calculate height of each rows and then summing them up.

I am doing in Xcode 7.2, iOS 9 and Swift 2

Abhinav
  • 3,322
  • 9
  • 47
  • 63

7 Answers7

2
var dictionaryOfCellHeight = [Int : CGFloat]()
var dataArray = [String]()
func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {


    if dictionaryOfCellHeight[indexPath.row] == nil
    {
    var frame : CGRect = tableView.rectForRowAtIndexPath(indexPath)

        if indexPath.row == dataArray.count
        {

            dictionaryOfCellHeight[indexPath.row] = frame.size.height
            //calculate total height and reload table
        }
        else
        {
        dictionaryOfCellHeight[indexPath.row] = frame.size.height
        }

    }
}
Mr. Bean
  • 4,221
  • 1
  • 19
  • 34
  • On executing this I am getting error in `if dictionaryOfCellHeight[indexPath.row] == nil` . Error is = warning: could not load any Objective-C class information. This will significantly reduce the quality of type information available. – Abhinav Jan 20 '16 at 14:15
  • call the code in cellforRow if you want , and the purpose of 'dictionaryOfCellHeight[indexPath.row] == nil ' is to check for duplicate key. The key is indexpath.row which should be unique. I have not tested the code – Mr. Bean Jan 21 '16 at 04:55
  • Tried that too, in cellForRow error does not come but app freezes and memory footprint of app keeps on increasing and goes beyond 2 GB and table never loads up – Abhinav Jan 21 '16 at 06:39
1

in Swift 3.0

let myRowHeight = yourTableView.rowHeight

Pavan Sisode
  • 61
  • 1
  • 8
0

To change the height of Tableview , you don't need to calculate the height of all UITableViewCells and set to TableView. Set the height of each UITableViewCell properly in heightForRowAtIndexPath method. This will automatically set the scrollable height to your Tableview

AskIOS
  • 918
  • 7
  • 19
  • Scroll is working fine. But I dont want scroll to happen. That is why trying to change height of `UITableView` – Abhinav Jan 20 '16 at 12:27
  • Whats the purpose of adding tableview if you don't want to have a scroll effect ? – AskIOS Jan 20 '16 at 12:32
  • 1
    Repeating same design for multiple records and doing some action on click on them. – Abhinav Jan 20 '16 at 12:42
  • @Abhi If you just want to prevent scrolling, why not use [scrollEnabled](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIScrollView_Class/#//apple_ref/occ/instp/UIScrollView/scrollEnabled)? – Mick MacCallum Jan 20 '16 at 12:58
  • 1
    @0x7fffffff in that also, I need to define height of my `UITableView` else some records might never become visible – Abhinav Jan 20 '16 at 13:07
0

You can use this

CGRect frame = [tableView rectForRowAtIndexPath:indexPath];
NSLog(@"row height : %f", frame.size.height);

Using frame.size.height you can get height of particular row

iAnurag
  • 9,286
  • 3
  • 31
  • 48
Mandeep Kumar
  • 776
  • 4
  • 18
0

This is because self.tableView.contentSize.height returns number of rows * estimatedRowHeight, which is not equivalent to actual height of the table view. What you need is to get the individual cell heights through visibleCells, then sum those up to get the table view's height.

Refer to this for the answer: https://stackoverflow.com/a/40081129/4076956

Community
  • 1
  • 1
leonardloo
  • 1,753
  • 3
  • 20
  • 31
0

Maybe the table is not resizing its content, so if you try using [self. tableView layoutSubviews]; to force the resizing, it may work. [self. tableView layoutIfNeeded]; not necessarily forces the update.

0

Functionally (one-liner)

NOTE: my section array stores the rows in the .data var

let heightOfAllRows:CGFloat = sections.enumerated().map { arg -> [(section:Int,row:Int)] in
            return arg.element.data.indices.map{ i in
                (section:arg.offset,row:i)
            }
        }.flatMap{$0}.reduce(0){
            return $0 + self.tableView(self, heightForRowAt: .init(row: $1.row, section: $1.section))
        }
Sentry.co
  • 5,355
  • 43
  • 38