【问题标题】:Add Header Title in a TableView for JSON data received using Paging在 TableView 中为使用分页接收的 JSON 数据添加标题标题
【发布时间】:2017-09-17 01:02:06
【问题描述】:

我已经搜索了很多,但我无法找到解决方案,有人可以指导我寻找以下解决方案。

使用页面 1 的分页接收的 JSON 数据,依此类推。

"ErrorCode":null,
"Message":"Success",
"Data":[
{
  "ProductID":1,
  "ProductName" :"Mango",
  "ProductCategory" :"Fruits"
},
{
  "ProductID":2,
  "ProductName" :"Banana",
  "ProductCategory" :"Fruits"
},
{
  "ProductID":3,
  "ProductName" :"Fanta",
  "ProductCategory" :"Drinks"
},
{
  "ProductID":4,
  "ProductName" :"Pepsi",
  "ProductCategory" :"Drinks"
},
{
  "ProductID":5,
  "ProductName" :"Carrot",
  "ProductCategory" :"Vegetables"
}

以上是我向上滚动时水果的示例数据,作为回报,我在使用分页时获得了多条记录,这是正确完成的,并且数据在表格视图中正确显示所有可用记录,如上所示。

我的要求是,我想为每个产品类别显示标题,例如标题将显示为水果,所有相关产品都将显示在下方。如果一次收到所有数据,我本可以这样做,但是当我们使用分页时,我不确定我在 json 响应中收到的下一页数据是否有另一个产品类别或相同的产品类别。

如果我的问题不清楚,请澄清。我希望做过分页的人知道我在说什么。

提前致谢。

【问题讨论】:

  • “我已经搜索了很多,但我无法找到解决方案”你花了多长时间?半秒?如果是这样,请继续搜索。或与具有使用搜索引擎运行搜索基本知识的朋友交谈。 SO 不是搜索服务。
  • 你需要Objective C还是swift的代码?

标签: ios objective-c json uitableview paging


【解决方案1】:

声明一个数据源,例如 self.dataSourceDictionary = [NSMutableDictionary dictionary]; 并过滤您的响应,如下所示(responseDictionary 包含 JSON 响应)

- (void)catagorizeData:(NSDictionary *)responseDictionary
{
    for (NSDictionary *productDictionary in [responseDictionary objectForKey:@"Data"]) {
        NSMutableArray *eachCategoryArray = [self.dataSourceDictionary objectForKey:[productDictionary objectForKey:@"productCategory"]];
        if (eachCategoryArray == nil) {
            eachCategoryArray = [NSMutableArray array];
            [eachCategoryArray addObject:productDictionary];
            [self.dataSourceDictionary setObject:eachCategoryArray forKey:[productDictionary objectForKey:@"productCategory"]];
        }else{
            [eachCategoryArray addObject:productDictionary];
        }
    }
}

每次收到分页响应时调用上述方法。 现在在tableview 部分显示过滤后的数据,如下所示:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.dataSourceDictionary count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *key = [[self.dataSourceDictionary allKeys] objectAtIndex:section];
    return [[self.dataSourceDictionary objectForKey:key] count];
}

注:这段代码只是为了给你一个基本的想法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 2021-10-01
    • 1970-01-01
    • 2017-05-27
    • 2021-04-29
    相关资源
    最近更新 更多