Here there is my simple viewDidLoad method and UITableView delegates in a ViewController:
import "HomeViewController.h"
import "AFNetworking.h"
import "SVPullToRefresh.h"
- (void)viewDidLoad
{
[super viewDidLoad];
[self parseJsonMethod];
items = [[NSMutableArray alloc] init]; // UPDATED CODE
items2 = [[NSMutableArray alloc] init]; // UPDATED CODE
[table addPullToRefreshWithActionHandler:^{
[self parseJsonMethod]; // <--- what's the number of NEW items after refreshing?
}];
}
- (void)parseJsonMethod{
// code to parse JSON file from the net ---> here I get NSMutableArray *items
NSURLRequest *requestJSON = [NSURLRequest requestWithURL:urlJSON];
AFJSONRequestOperation *operationJSON = [AFJSONRequestOperation
JSONRequestOperationWithRequest:requestJSON
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSMutableArray *results = [JSON valueForKey:@"results"];
// UPDATED CODE
for (id obj in [results valueForKey:@"value"]) {
if (![items containsObject:obj]) {
[items addObject:obj];
}
}
for (id obj2 in [results valueForKey:@"value2"]) {
if (![items2 containsObject:obj2]) {
[items2 addObject:obj2];
}
}
// END OF UPDATED CODE
[table reloadData];
[table scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
[table.pullToRefreshView stopAnimating];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"%@", [error userInfo]);
}];
[operationJSON start];
[table reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [items count]; // <--- items is the NSMutableArray parsed from JSON file on the net: HERE I HAVE THE PROBLEM, ITS ALWAYS 10!
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:
UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}
cell.textLabel.text = [NSString stringWithFormat:@"%@",[items objectAtIndex:indexPath.row]];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",[items2 objectAtIndex:indexPath.row]];
return cell;
}
As u can see I have implemented SVPullToRefresh classes to refresh data after pulling the table, and this is perfect: after pulling I can see new data but on the SAME rows (the number of rows is 10 before and after refreshing). I'd like to add NEW rows after pulling table and new parsing, and mantain the old rows with old data (every time 10 + new items + ...). There is a simple way to do that? or at least to recognize the number of new items? maybe implementing insertRowsAtIndexPaths delegate? Please help me.
UPDATE: see new edited code, I have declared previously items but there is still some problem.