apollo9
apollo9

Reputation: 131

how many time heightForRowAtIndexPath being called before view Presentation? Objective-c

I'm developing an iPad app, and I have found this problem, why when I present tableView , the method heightForRowAtIndexPath it's called more than one time for each row?

For Example if I have ten row, the method will be calls 30 times.

Upvotes: 1

Views: 373

Answers (2)

Ketan Parmar
Ketan Parmar

Reputation: 27438

Every time a table view is displayed, it calls tableView:heightForRowAtIndexPath: on the delegate for each of its rows. so if you scroll your tableview then this method get called, when system layouting your cell then also this method get called.

Refer apple documentation,

The method allows the delegate to specify rows with varying heights. If this method is implemented, the value it returns overrides the value specified for the rowHeight property of UITableView for the given row.

There are performance implications to using tableView:heightForRowAtIndexPath: instead of the rowHeight property. Every time a table view is displayed, it calls tableView:heightForRowAtIndexPath: on the delegate for each of its rows, which can result in a significant performance problem with table views having a large number of rows (approximately 1000 or more).

So, it's better to not implement this method when there are too much rows!

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726889

Unlike numberOfSectionsInTableView which is called once, and numberOfRowsInSection, which is called once per section, heightForRowAtIndexPath is called for each row. Generally, the component needs to find out how many rows it needs to create (which translates to how many row cells it needs to allocate) so it keeps calling heightForRowAtIndexPath until the total height is sufficient to cover the area of the screen, plus a few more rows for the scroll.

In your situation it looks like the screen is high enough to fit some 30 rows of your table; hence, you get thirty calls to heightForRowAtIndexPath. These calls will be followed by roughly as many calls to cellForRowAtIndexPath.

Upvotes: 2

Related Questions