Below is the function that i am using to get and parse different pages of the feed.
- (void)getFeed:(NSInteger) pageNumber
{
collectedData = [[NSMutableData alloc] init];
NSString *urlStr =[NSString stringWithFormat:@"http://sitename.com/feed/?paged=%d", pageNumber];
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];
}
When user scrolls down to the bottom of tableview, i use the below function to access the second page of feed and so on by incrementing the counter:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
float endScrolling = scrollView.contentOffset.y + scrollView.frame.size.height;
if (endScrolling >= scrollView.contentSize.height)
{
counter ++;
[self getFeed:counter];
}
}
The issue is when instead of loading it below the already fetched items, it reloads the tableview to show the new page items and old ones disappear. I want to load new page items below the existing ones to have infinite scroll. How can i do that?
It feels bad to answer your own question, but taking help from another forum, i was able to figure out the answer.
The tableview controller was not in sync with the mutable array, so i created another and append both arrays.
Step1: I created a property
@property (nonatomic, readonly) NSMutableArray *moreItems;
Step2: Initialised the array in ViewDidLoad before calling fetchEntries
moreItems = [[NSMutableArray alloc] init];
Step3: In connectionDidFinishLoading, i appended new array 'moreItems' to the previous array 'items'
[moreItems addObjectsFromArray:[channel items]];
Step4: Changed the data source in numberOfRowsInSection from
return [[channel items] count];
to new array
return [moreItems count];
and then in cellForRowAtIndexPath, used the new array
RSSItem *item = [moreItems objectAtIndex:[indexPath row]];
You should use insertRowsAtIndexPaths: withRowAnimation:
method of UITableView
.
One thing you must take care of is that when you use this method, number rows retuned by tableView:numberOfRowsInSection
should be equal to number_of_existing_rows + number_of_newly_inserted_rows
After you finish retrieving the new feed for next page get the objects in an array. Add these objects from this array to your data source array. Now insert rows to the table as follows:
[yourTable beginUpdates];
[yourTable insertRowsAtIndexPaths:indexPathsToInsert
withRowAnimation:UITableViewRowAnimationBottom];
[yourTable endUpdates];
where "indexPathsToInsert" is array of NSIndexPath objects starting from the last row in your table and containg indexPaths for your new objects.
Hope this helps.
Hi,
Create a for loop, with the starting index is (number of objects in datasource) and number of objects you want to add (count),create the array of indexpaths and call insertRowsAtIndexpaths:withRowAnimation. I hope the code helps you.
NSInteger statingIndex = [self.tableViewDatasource count];
NSInteger noOfObjects = //Get the no.of objects count from getFeed method.
NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
for (NSInteger index = statingIndex; index < statingIndex+noOfObjects; index++) {
[_objects addObject:]; // add the object from getFeed method.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
[indexPaths addObject:indexPath];
[indexPath release];
}
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With