在這裏有我的簡單viewDidLoad方法和UITableView的代表在一個視圖控制器:插入在UITableView的新行刷新表後
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;
}
就像你看到的我已經實現SVPullToRefresh類拔表後刷新數據,並這是完美的:拉動後,我可以看到新的數據,但在同一行(刷新之前和之後的行數是10)。我想在拉動表格和新解析之後添加新行,並用舊數據(每次10 +新項目+ ...)維護舊行。有一個簡單的方法來做到這一點?或者至少能夠識別新項目的數量?也許實現insertRowsAtIndexPaths委託?請幫幫我。
更新:看到新的編輯代碼,我已經聲明瞭以前的項目,但仍有一些問題。
打印[項目數]調用reloadData上的tableView前parseJsonMethod {}安慰。它是否顯示不同數量的項目? – 0x8badf00d
不,其始終10 – Huxley
檢查此https://gist.github.com/4277574 – 0x8badf00d