Search code examples
iosobjective-cxml-parsingnsmutablearrayfeed

append data to start of NSMutableArray


I have app where I am showing the latest conversation between two users.

For that I am using XML feeds data.

Let's say I have 25 rows in one XML and url goes like below.

www.mywebsite.com/getChat.asmx?startIndex=0

When I click loadMore button, the link goes like below.

www.mywebsite.com/getChat.asmx?startIndex=1

In this again I get 25 rows.

Now I have problem in displaying the data.

When I open the message, with startIndex=0, I am putting latest message at the bottom. Means I am getting data in below format.

4 hours ago...
3 hours ago...
2 hours ago...
44 mins ago...
etc

Now when I click loadmore button (startIndex=0), I am appending the XML data in my feeds (NSMutableArray). But all the new chat come to down instead of at the top.

I understand this is happening because I am appening the data at the end.

Any idea how can I append the data at the start of the feed so that the message between two users goes properly in tableview?


In short, I already have the data in NSMutableArray (let' say of size 20). And I want to add some data (let's say of length 10).

So new data will be at position 0-9 and old data that was there at 0-19 will get shifted to 10-29 as new data is added.

Any idea how to do this?


Solution

  • First get data in NSMutableArray and reverse the objects of your Array by using below code:

    reversedArray = [[theMutableArray reverseObjectEnumerator] allObjects];