Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView :cellForRowAtIndexPath Continues being called

I have a UITableView Controller and a UITableView. I have everything set up using the delegates etc, and it populates fine. I notice a small bug however with the following method:

:cellForRowAtIndexPath

I am noticing that this method is continually called every time I scroll the table. Even after the table is populated, it continues to call. Basically, a cell moves out of view, when it comes back in view, it is calling it again. I had NSLog print out the cell contents within that method, which is how I know it continues to call.

Should that function not just call once per cell, to populate it, then be done?

like image 686
Chris Avatar asked Mar 03 '11 21:03

Chris


1 Answers

Nope. That's called every time a cell is needed, and a cell is needed every time one is rendered.

UITableView is a very clever little critter. It literally keeps only the cells it's displaying, plus a cache of literally two or three more. When the time comes to scroll a cell on that wasn't on, it can be asked for a cell from the cache, and if there is one, it gets reused.

This way HUGE data sets can be displayed in a UITableView, because there really aren't a large number of cells in memory, only the ones on the screen and a small cache.

like image 56
Dan Ray Avatar answered Oct 26 '22 20:10

Dan Ray