SimplyKiwi
SimplyKiwi

Reputation: 12444

Iterate for loop backwards?

I am pretty much trying to do the following line with backwards iteration:

for (int i = 0; i < [thetableView numberOfRowsInSection:0]; i++) {

I know there is reverseObjectEnumerator but I am not sure how to integrate that into an array like this. Can anyone help me out here? I have looked through many other answers to no avail.

Would this do it?

for (int i = [thetableView numberOfRowsInSection:0]-1; i > -1; i--) {

Upvotes: 0

Views: 435

Answers (1)

Jonathan
Jonathan

Reputation: 2383

int numberOfRows = [theTableView numberOfRowsInSection:0] - 1;

for (int i = numberOfRows; i >= 0; i--)
{

}
  1. Set the variable to the number of rows, minus 1 (to account for the 0 base).

  2. Set the condition to test if i is greater than or equal to 0. If the above condition is true,

  3. Instead of adding 1 on each iteration, decrease by 1.

Upvotes: 3

Related Questions