views:

538

answers:

2

I have an UITableView set up with a NSArray with 10 indexes. Right now, the first cell on the uitableview is the first index, the second cell is the second index, and so on so forth. Is there any way to make it so that the first cell displays the latest index? Maybe through some code in the uitableview delegate because I am adding data to the NSArray. What that means is that there aren't 10 indexes right off the bat.

If anyone as an answer, help is much appreciated.

+1  A: 

Hi

Each time that you get a new item of data, you add it to the start of your array, not to the end. Then just call [self.tableView reloadData] and it should just work.

You can use insertObject:atIndex: to add to the start of the array:

[myArray insertObject:newData atIndex:0];

(see here for docs)

deanWombourne
Currently, I'm adding the new objects like this:[array addObject:object];How would I have to change this piece of code to add to the start?
intl
You can use insertObject:atIndex: - see my edited answer.
deanWombourne
Thanks so much. Works perfectly. :)
intl
A: 

Somewhere in your code you're probably doing something like this (where items is your NSArray object):

cell.textLabel.text = [items objectAtIndex:indexPath.row];

Instead, do:

cell.textLabel.text = [items objectAtIndex:([items count] - 1) - indexPath.row];
Frank Schmitt