【问题标题】:Insert new rows in UITableView after refreshing table刷新表格后在 UITableView 中插入新行
【发布时间】:2012-12-13 12:09:04
【问题描述】:

这是我在 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 委托?请帮帮我。

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

【问题讨论】:

  • 在 tableView 上调用 reloadData 之前,将 [items count] 打印到 parseJsonMethod{} 中的控制台。它是否显示不同的项目数?
  • 谢谢 0x8badf00d,不幸的是它是同一件事:我看不到相同数组编号的 value 和 value2 之间的精确对应关系(我有一个项目的文本和另一个项目的 detailText 的行),有时应用程序因“索引超出范围 [0 .. 9]”错误而崩溃...

标签: xcode uitableview pull-to-refresh


【解决方案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"]];

【讨论】:

  • 嘿@Ryan,谢谢你,我现在编辑了我的问题:JSON 解析是一个带有 AFNetworking 类的简单代码。
  • 不幸的是,第一种方法(对于 id obj)给出 0 行,而“超基本”方法总是给出 10 行
  • 这意味着你没有得到新的行。
  • 在这些线上 NSMutableArray *results = [JSON valueForKey:@"results"]; items = [results valueForKey:@"value"] 我不确定你在做什么。您将 valueForKey 发送到索引未键控的数组。所以[results valueForKey:@"value"] 可能总是空的。我会记录 NSLog(@"%@",results); 并查看您的数据到底在哪里。
  • 如果您有 10 件商品。你再添加 10 个对象,即 20 个。大声笑。因此,[results valueForKey:@"value"] 并不总是 10,或者您没有添加它们而是替换它们。不是想成为一个混蛋,但如果它总是十个并且你继续像这样[items addObjectsFromArray:[results valueForKey:@"value"]];添加它们,那么它不可能不会增加。
猜你喜欢
  • 2015-03-03
  • 2014-08-11
  • 2015-12-28
  • 2013-12-02
  • 2011-12-05
  • 2014-09-06
  • 2021-04-12
  • 2013-09-22
  • 2012-04-27
相关资源
最近更新 更多