Reputation: 509
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.
Upvotes: 0
Views: 1243
Reputation: 18561
What you're doing does add new rows if its given new rows. Your items array is the problem here. Whatever data your getting from the JSON source and however you're adding it to the items array is the issue.
If you need more help show us your JSON code and where the items array gets populated both originally and on reloads. If there aren't new rows in items they won't be in the table.
Update
On this line: items = [results valueForKey:@"value"]; // (initial number of items = 10)
I'd try something like this:
for (id obj in [results valueForKey:@"value"]) {
if (![items containsObject:obj]) {
[items addObject:obj];
}
}
This will check for duplicate items and add them to the ones you already have. Now there could still be an issue if your JSON feed returns 10 of the same item and you won't see a difference.
For ultra basic just to see it working just [items addObjectsFromArray:[results valueForKey:@"value"]];
Upvotes: 1