2012-12-13 26 views
0

在這裏有我的簡單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委託?請幫幫我。

更新:看到新的編輯代碼,我已經聲明瞭以前的項目,但仍有一些問題。

+0

打印[項目數]調用reloadData上的tableView前parseJsonMethod {}安慰。它是否顯示不同數量的項目? – 0x8badf00d

+0

不,其始終10 – Huxley

+0

檢查此https://gist.github.com/4277574 – 0x8badf00d

回答

1

如果您正在做的是添加新行,如果給定的新行。你的物品數組是這裏的問題。無論您從JSON源獲得的數據是什麼,但是您將其添加到items數組都是問題所在。

如果您需要更多幫助,請向我們展示您的JSON代碼以及項目數組在原來和重新加載時的位置。如果項目中沒有新行,它們將不在表格中。

更新

在此行中:items = [results valueForKey:@"value"]; // (initial number of items = 10)

我想嘗試這樣的事:

for (id obj in [results valueForKey:@"value"]) { 
    if (![items containsObject:obj]) { 
     [items addObject:obj]; 
    } 
} 

這將檢查重複的項目,並把它們添加到你已經擁有的那些。現在,如果您的JSON供稿返回了同一項目的10個,您仍然可能會遇到問題,並且您不會看到差異。

對於超基本只是爲了看看它的工作只是[items addObjectsFromArray:[results valueForKey:@"value"]];

+0

嘿@Ryan,謝謝,我現在編輯我的問題:JSON解析是一個帶有AFNetworking類的簡單代碼。 – Huxley

+0

不幸的是,第一種方法(id obj)給出了0行,「超基本」方法總是給出10行 – Huxley

+0

這意味着你沒有得到新的行。 –