views:

112

answers:

2

I want to verify if I'm in the last cell of the UITable. Should I use a NSInteger property in my controller and store the value of the count of the data (it's an NSMutableArray) that populates the table?

I'm asking because I don't know if count iterates over all objects of the array to count or if it only gets me a value of a count property.

What would be better ?

store the value: myCount = [myArray count]
use directly: [myArray count]
store numberOfRows: myCount = numberOfRowsInSection:[indexPath 0]
use numberOfRows directly: numberOfRowsInSection:[indexPath 0]

+1  A: 

Because the state of the table view can change at any time, depending on what the iPhone OS is doing at any given time, you will almost always want to pull state data from your model (from the array, in this case).

Alex Reynolds
+3  A: 

Use the method directly. It is almost certainly efficient enough. If you are having serious performance problems, use shark and instruments to discover why. If you see most of your time is being spent in calls to count, find a way to optimize that, like you suggested here. But don't optimize prematurely like this--you are just wasting your time even thinking about it.

Jason Coco
Ok, I was thinking that it could compromise the performance. I always deal with this issues in my head since I started programming with C.
Rafael Oliveira
Even in C, the performance impact to calling a function is very small (much smaller than in Objective-C). When you tune for performance it's much better to write the code as solidly as possible then specifically test for where your real performance bottlenecks are (again, even in C). Modern compilers and computer architecture are a lot better at optimizing than they were 15 years ago, so let them focus on the first round of optimizations while you focus on the first round of problem solving, then tune :)
Jason Coco