Wun
Wun

Reputation: 6381

When does objective-C call the cellForRowAtIndexPath?

I am new to iOS development, and study about Bluetooth Low Energy (BLE, Bluetooth 4.0) for IOS.

I am try to add the 10 data to tableView.

From the log , it will run 10 time for cellForRowAtIndexPath.

The question is :

How the objective-c know it have to run 10 time for cellForRowAtIndexPath ?

Is it according to the numberOfRowsInSection ?

Upvotes: 2

Views: 574

Answers (2)

Hermann Klecker
Hermann Klecker

Reputation: 14068

cellForRowAtIndexPath is called every time when a cell comes to view.

Don't expect it to be called a certain times nor expect any specific sequence.

Example: There is one section only with 15 rows. Therefore the table will have 15 cells in total - from the user's point of view. numberOfRowsInSection will return 15.

Let's say that 10 of them are visible on an iPhone 5 and 9 of them are visible on an iPhone 4. At start cellForRowAtIndexPath is called 9 times on an iPhone 4 and 10 times on an iPhone 5.

Let's continue with the iPhone 5. The user now scrolls to the bottom of the table. 5 more cells come into view. Then cellForRowAtIndexPath is called 5 times. BTW 4 of these cells that disappeared earlier can and will be reused. (given that all cells are of the same type and reuse-identifier)

So far cellForRowAtIndexPath was called once for each cell.

Now the user scrolls up again to the top of the table. cellForRowAtIndexPath is now called again for cell of row 4, 3, 2, 1 and 0.

So far cellForRowAtIndexPath was called 20 times for 15 cells.

Now the user ...

Upvotes: 6

Ramdhas
Ramdhas

Reputation: 1765

cellForRowAtIndexPath will be call, Whenever the view displaying or changing the tableView.

For your clear understand put a breakpoint in cellForRowAtIndexPath method then run, you'll know that how it called.

Upvotes: 0

Related Questions