I'm trying to add 10 more records to a UITableView
when the user scrolls to the bottom of the tableView. I'm using scrollViewDidEndDecelerating
: and then a -(void)getFeed:
to get the data and store it in a NSMutableArray
called moreitems
. moreitems
should then be appended to self.listingNodesArray
and then appended to the bottom of the table along with the records that are already there in the table.
I think my problem lies with the insertRowsAtIndexPaths:
method. I'm not sure what to put after that. At the moment the app crashes on [self.tableView endUpdates];
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@"scrollViewDidEndDecelerating");
float endScrolling = scrollView.contentOffset.y + scrollView.frame.size.height;
if (endScrolling >= scrollView.contentSize.height)
{
index ++;
[self getFeed:index];
}
}
- (void)getFeed:(NSInteger) pageNumber
{
NSError *requestError = nil;
NSString *savedValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"token"];
NSLog(@"countpage %d",index);
NSString *stringWithToken = [NSString stringWithFormat:@"%@&%@&page=%d&token=%@",kURL,city_slug, pageNumber, savedValue];
NSLog(@"string token is %@", stringWithToken);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL
URLWithString:stringWithToken]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&requestError];
NSError *jsonParsingError = nil;
if (requestError) {
NSLog(@"sync. request failed with error: %@", requestError);
}
else {
// handle data
NSDictionary *publicData = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];
moreItems = [publicData objectForKey:@"data"];
}
NSLog(@"moreitems data output is %@",moreItems);
[self.listingNodesArray addObjectsFromArray:moreItems];
int newRowIndex = [self.listingNodesArray count];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:newRowIndex+1 inSection:0];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationBottom];
[self.tableView endUpdates];
NSLog(@"updated self.listingNodesArray %@ and count is %d",self.listingNodesArray, [self.listingNodesArray count]);
}
thanks for any help with this.