0

这是我在 ViewController 中的简单 viewDidLoad 方法和 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 委托?请帮我。

更新:查看新编辑的代码,我已经声明了以前的项目,但仍然存在一些问题。

4

1 回答 1

1

如果给定新行,您正在做的事情会添加新行。您的 items 数组是这里的问题。无论您从 JSON 源获得什么数据,以及将其添加到 items 数组中,都是问题所在。

如果您需要更多帮助,请向我们展示您的 JSON 代码以及最初和重新加载时填充 items 数组的位置。如果项目中没有新行,它们就不会出现在表中。

更新

在这条线上: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"]];

于 2012-12-13T13:29:38.300 回答