2017-09-17 27 views
0

我已经搜索了很多,但我无法找到解决方案,有人可以引导我解决以下问题。在TableView中添加标题标题以查看使用Paging收到的JSON数据

使用页面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响应中收到的下一页数据是否有另一个产品类别或相同。

请澄清我的问题是否不清楚。我希望已经完成分页的人知道我在说什么。

在此先感谢。

+0

“我搜索了很多,但我无法找到解决方案”你花了多长时间?半秒钟?如果是这样,继续搜索。或者与有搜索引擎运行搜索基本知识的朋友交谈。 SO不是搜索服务。 –

+0

您是否需要Objective C或Swift中的代码? – Vinodh

回答

1

e.g 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]; 
} 

N.B此代码只是为了给你一个基本思路。