如何在构建可扩展的tableview时区分json响应数组

Posted

技术标签:

【中文标题】如何在构建可扩展的tableview时区分json响应数组【英文标题】:how to differentiate json Response array while building an expandable tableview 【发布时间】:2016-09-14 07:16:10 【问题描述】:

使用以下形式的响应响应:


  "category_tree": [
    
      "id": "1",
      "name": "Electronics",
      "parent_category_code": "0",
      "status": "approved",
      "language_id": "1",
      "cat_code": "20160705014449-127001FfboIICtZe",
      "ddate_time": "2016-07-05 01:44:49",
      "sub_cat": [
        
          "id": "3",
          "name": "Mobile english en",
          "parent_category_code": "20160705014449-127001FfboIICtZe",
          "status": "approved",
          "language_id": "1",
          "cat_code": "20160705014604-1270010DFNU8bays",
          "ddate_time": "2016-07-05 01:46:04",
          "sub_sub_cat": [
            
              "id": "9",
              "name": "Motorola",
              "parent_category_code": "20160705014604-1270010DFNU8bays",
              "status": "approved",
              "language_id": "1",
              "cat_code": "20160705022145-127001EewzhgWG5Y1",
              "ddate_time": "2016-07-05 02:21:45"
            ,
            
              "id": "31",
              "name": "apple",
              "parent_category_code": "20160705014604-1270010DFNU8bays",
              "status": "approved",
              "language_id": "1",
              "cat_code": "20160820125103-37106451660Cw9bmocor",
              "ddate_time": "2016-08-20 12:51:03"
            
          ]
        
      ]
    ,
    
      "id": "5",
      "name": "Automotive",
      "parent_category_code": "0",
      "status": "approved",
      "language_id": "1",
      "cat_code": "20160705022124-127001OqNHlzDGHL",
      "ddate_time": "2016-07-05 02:21:23",
      "sub_cat": [
        
          "id": "7",
          "name": "Hydraulik",
          "parent_category_code": "20160705022124-127001OqNHlzDGHL",
          "status": "approved",
          "language_id": "1",
          "cat_code": "20160705022145-127001EewzhgWG5Y",
          "ddate_time": "2016-07-05 02:21:45",
          "sub_sub_cat": [
            
              "id": "11",
              "name": "Fluidic",
              "parent_category_code": "20160705022145-127001EewzhgWG5Y",
              "status": "approved",
              "language_id": "1",
              "cat_code": "20160705022145-127001EewzhgWG5Y2",
              "ddate_time": "2016-07-05 02:21:45"
            
          ]
        
      ]
    ,
    
      "id": "13",
      "name": "Fashion",
      "parent_category_code": "0",
      "status": "approved",
      "language_id": "1",
      "cat_code": "20160705014449-127001FfboIICtZeF",
      "ddate_time": "2016-07-05 01:44:49",
      "sub_cat": [
        
          "id": "17",
          "name": "MEN",
          "parent_category_code": "20160705014449-127001FfboIICtZeF",
          "status": "approved",
          "language_id": "1",
          "cat_code": "20160812125126-1270011lllHsWx9H",
          "ddate_time": "2016-08-12 12:51:26",
          "sub_sub_cat": [
            
              "id": "21",
              "name": "Footware",
              "parent_category_code": "20160812125126-1270011lllHsWx9H",
              "status": "approved",
              "language_id": "1",
              "cat_code": "20160812125510-127001S8hVEJynOR",
              "ddate_time": "2016-08-12 12:55:10"
            ,
            
              "id": "23",
              "name": "Partyware",
              "parent_category_code": "20160812125126-1270011lllHsWx9H",
              "status": "approved",
              "language_id": "1",
              "cat_code": "20160812010851-1270011LQhUSZgaN",
              "ddate_time": "2016-08-12 01:08:51"
            
          ]
        ,
        
          "id": "19",
          "name": "WOMEN",
          "parent_category_code": "20160705014449-127001FfboIICtZeF",
          "status": "approved",
          "language_id": "1",
          "cat_code": "20160812125146-1270012tW7VrQH10",
          "ddate_time": "2016-08-12 12:51:46",
          "sub_sub_cat": [
            
              "id": "27",
              "name": "Partyware",
              "parent_category_code": "20160812125146-1270012tW7VrQH10",
              "status": "approved",
              "language_id": "1",
              "cat_code": "20160812012407-1270011ZU5fZUHcz",
              "ddate_time": "2016-08-12 01:24:07"
            ,
            
              "id": "29",
              "name": "Nightware",
              "parent_category_code": "20160812125146-1270012tW7VrQH10",
              "status": "approved",
              "language_id": "1",
              "cat_code": "20160812012730-127001kpxQOh3oGP",
              "ddate_time": "2016-08-12 01:27:30"
            
          ]
        ,
        
          "id": "61",
          "name": "Living Room",
          "parent_category_code": "20160705014449-127001FfboIICtZeF",
          "status": "approved",
          "language_id": "1",
          "cat_code": "20160825112226-180211107130NAoAdes7uY",
          "ddate_time": "2016-08-25 11:22:26"
        
      ]
    ,
    
      "id": "33",
      "name": "food",
      "parent_category_code": "0",
      "status": "approved",
      "language_id": "1",
      "cat_code": "20160820125200-37106451661w1jVnnD78",
      "ddate_time": "2016-08-20 12:52:00"
    ,
    
      "id": "35",
      "name": "Mobiles & Tablets",
      "parent_category_code": "0",
      "status": "approved",
      "language_id": "1",
      "cat_code": "20160825083005-18021110713003bgVwjLTJ",
      "ddate_time": "2016-08-25 08:30:05"
    ,
    
      "id": "37",
      "name": "Men's Fashion",
      "parent_category_code": "0",
      "status": "approved",
      "language_id": "1",
      "cat_code": "20160825083757-1802111071301YBFaJXfga",
      "ddate_time": "2016-08-25 08:37:56",
      "sub_cat": [
        
          "id": "49",
          "name": "Shirts",
          "parent_category_code": "20160825083757-1802111071301YBFaJXfga",
          "status": "approved",
          "language_id": "1",
          "cat_code": "20160825110456-180211107130cMoRzTjkoc",
          "ddate_time": "2016-08-25 11:04:56"
        ,
        
          "id": "51",
          "name": "Tshirts",
          "parent_category_code": "20160825083757-1802111071301YBFaJXfga",
          "status": "approved",
          "language_id": "1",
          "cat_code": "20160825110559-180211107130Hh5RpcYXm9",
          "ddate_time": "2016-08-25 11:05:59"
        ,
        
          "id": "53",
          "name": "Men's Watches",
          "parent_category_code": "20160825083757-1802111071301YBFaJXfga",
          "status": "approved",
          "language_id": "1",
          "cat_code": "20160825110732-1802111071300RJEpM0Hfj",
          "ddate_time": "2016-08-25 11:07:32"
        
      ]
    ,
    
      "id": "39",
      "name": "Motors",
      "parent_category_code": "0",
      "status": "approved",
      "language_id": "1",
      "cat_code": "20160825083827-18021110713000kPycVWfp",
      "ddate_time": "2016-08-25 08:38:27"
    ,
    
      "id": "41",
      "name": "Home & Kitchen",
      "parent_category_code": "0",
      "status": "approved",
      "language_id": "1",
      "cat_code": "20160825083901-1802111071301eRlC0fDct",
      "ddate_time": "2016-08-25 08:39:01",
      "sub_cat": [
        
          "id": "59",
          "name": "Living Room",
          "parent_category_code": "20160825083901-1802111071301eRlC0fDct",
          "status": "approved",
          "language_id": "1",
          "cat_code": "20160825112047-1802111071301QjtsywNm6",
          "ddate_time": "2016-08-25 11:20:47"
        
      ]
    ,
    
      "id": "43",
      "name": "Daily Needs",
      "parent_category_code": "0",
      "status": "approved",
      "language_id": "1",
      "cat_code": "20160825085534-1802111071301a3xWV7DRo",
      "ddate_time": "2016-08-25 08:55:34",
      "sub_cat": [
        
          "id": "69",
          "name": "Hair Oils",
          "parent_category_code": "20160825085534-1802111071301a3xWV7DRo",
          "status": "approved",
          "language_id": "1",
          "cat_code": "20160825113622-1802111071300TPLT5QVL9",
          "ddate_time": "2016-08-25 11:36:22",
          "sub_sub_cat": [
            
              "id": "71",
              "name": "soaps",
              "parent_category_code": "20160825113622-1802111071300TPLT5QVL9",
              "status": "approved",
              "language_id": "1",
              "cat_code": "20160825114003-1802111071300l6CNncEXE",
              "ddate_time": "2016-08-25 11:40:03"
            
          ]
        
      ]
    ,
    
      "id": "45",
      "name": "Sports",
      "parent_category_code": "0",
      "status": "approved",
      "language_id": "1",
      "cat_code": "20160825085803-180211107130WR3tRcEEIy",
      "ddate_time": "2016-08-25 08:58:03",
      "sub_cat": [
        
          "id": "55",
          "name": "Cricket",
          "parent_category_code": "20160825085803-180211107130WR3tRcEEIy",
          "status": "approved",
          "language_id": "1",
          "cat_code": "20160825111232-180211107130ClhKEE0HRY",
          "ddate_time": "2016-08-25 11:12:31",
          "sub_sub_cat": [
            
              "id": "63",
              "name": "Bat",
              "parent_category_code": "20160825111232-180211107130ClhKEE0HRY",
              "status": "approved",
              "language_id": "1",
              "cat_code": "20160825112329-180211107130TCGNsByhHY",
              "ddate_time": "2016-08-25 11:23:29"
            ,
            
              "id": "65",
              "name": "Ball",
              "parent_category_code": "20160825111232-180211107130ClhKEE0HRY",
              "status": "approved",
              "language_id": "1",
              "cat_code": "20160825112442-18021110713012uxH5Grol",
              "ddate_time": "2016-08-25 11:24:41"
            
          ]
        ,
        
          "id": "57",
          "name": "Golf",
          "parent_category_code": "20160825085803-180211107130WR3tRcEEIy",
          "status": "approved",
          "language_id": "1",
          "cat_code": "20160825111407-180211107130GmoE48njSU",
          "ddate_time": "2016-08-25 11:14:07"
        
      ]
    ,
    
      "id": "47",
      "name": "Toys",
      "parent_category_code": "0",
      "status": "approved",
      "language_id": "1",
      "cat_code": "20160825085931-18021110713002SmprXJw2",
      "ddate_time": "2016-08-25 08:59:31"
    
  ]

我如何通过获取所有这些类别来扩展 tableview? 我已经成功地扩展了表格视图,所有这些项目在它的级别上都是正确的。但我不知道如何为每个带有类别的单元格设置setIndentationLevel

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    return 1;


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    return [self.arForTable count];


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
    
    cell.textLabel.text=[[self.arForTable objectAtIndex:indexPath.row] valueForKey:@"name"];
    NSDictionary *d = [self.arForTable objectAtIndex:indexPath.row];

    if (ForMainCat)
    
        if ([d valueForKey:@"sub_cat"])
        
            cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"Plus.png"]];
            [cell.accessoryView setFrame:CGRectMake(0, 0, 20, 20)];
            [cell setIndentationLevel:0];
        
    
    if (ForSubCat)
    
        if ([d valueForKey:@"sub_sub_cat"])
        
            cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"Plus.png"]];
            [cell.accessoryView setFrame:CGRectMake(0, 0, 20, 20)];
            [cell setIndentationLevel:1];
        
    
    if (ForSubSubCat)
    
        [cell setIndentationLevel:2];
    

    /*if ([d valueForKey:@"sub_cat"])
    
        cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"Plus.png"]];
        [cell.accessoryView setFrame:CGRectMake(0, 0, 20, 20)];
        [cell setIndentationLevel:0];
    
    else if ([d valueForKey:@"sub_sub_cat"])
    
        cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"Plus.png"]];
        [cell.accessoryView setFrame:CGRectMake(0, 0, 20, 20)];
        [cell setIndentationLevel:1];
    
    else if ([d ])
    
        [cell setIndentationLevel:2];
    */
    return cell;


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    NSDictionary *d = [self.arForTable objectAtIndex:indexPath.row];
    if (ForMainCat)
    
        ForSubCat = true;
        ForSubSubCat = false;
        ForMainCat = false;
        NSArray *ar=[d valueForKey:@"sub_cat"];
        [self MaximizeThisRows:ar :indexPath];
    
    if(ForSubCat)
    
        ForMainCat = false;
        ForSubSubCat = true;
        ForSubCat = false;
        NSArray *ar=[d valueForKey:@"sub_sub_cat"];
        [self MaximizeThisRows:ar :indexPath];
    
    else if (ForSubSubCat)
    
        ForMainCat = false;
        ForSubCat = false;
        ForSubSubCat = true;
        NSArray *ar=[d valueForKey:@"sub_sub_cat"];
        [self MaximizeThisRows:ar :indexPath];
    

#pragma mark - Open SUb Cell
-(void)MaximizeThisRows : (NSArray*)ar :(NSIndexPath*)indexPath

    UITableViewCell *cell = [self.subCatTbl cellForRowAtIndexPath:indexPath];
    BOOL isAlreadyInserted=NO;

    for(NSDictionary *dInner in ar)
    
        NSInteger index=[self.arForTable indexOfObjectIdenticalTo:dInner];
        isAlreadyInserted=(index>0 && index!=NSIntegerMax);
        if(isAlreadyInserted) break;
    
    if(isAlreadyInserted)
    
        [self miniMizeThisRows:ar];
        cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"Plus.png"]];
        [cell.accessoryView setFrame:CGRectMake(0, 0, 20, 20)];
    
    else
    
        NSUInteger count=indexPath.row+1;
        NSMutableArray *arCells=[NSMutableArray array];
        for(NSDictionary *dInner in ar )
        
            [arCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
            [self.arForTable insertObject:dInner atIndex:count++];
            cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"Minus.png"]];
            [cell.accessoryView setFrame:CGRectMake(0, 0, 20, 20)];
        
        [self.subCatTbl insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationAutomatic];
    

#pragma mark - Close Sub Cell
-(void)miniMizeThisRows:(NSArray*)ar

    for(NSDictionary *dInner in ar)
    
        NSUInteger indexToRemove=[self.arForTable indexOfObjectIdenticalTo:dInner];
        NSArray *arInner;
        if ([dInner valueForKey:@"sub_cat"])
        
            arInner=[dInner valueForKey:@"sub_cat"];
        
        else
        
            arInner=[dInner valueForKey:@"sub_sub_cat"];
        
        if(arInner && [arInner count]>0)
        
            [self miniMizeThisRows:arInner];
        

        if([self.arForTable indexOfObjectIdenticalTo:dInner]!=NSNotFound)
        
            [self.arForTable removeObjectIdenticalTo:dInner];
            [self.subCatTbl deleteRowsAtIndexPaths:[NSArray arrayWithObject:
                    [NSIndexPath indexPathForRow:indexToRemove inSection:0]]
                     withRowAnimation:UITableViewRowAnimationAutomatic];
        
    

#pragma mark - Get Category list
-(void)getCategory

    //http://180.211.107.130:8080/mahlati/api/apicall/get_category_tree
    NSString *setUrl = [NSString stringWithFormat:@"%@get_category_tree",webUrl;
    NSString *languageId = [[[Defualt valueForKey:@"AppleLanguages"]objectAtIndex:0] isEqualToString:@"en"] || [[[Defualt valueForKey:@"AppleLanguages"]objectAtIndex:0] isEqualToString:@"en-US"]? @"1" : @"2";
    NSMutableDictionary *parameters = [[NSMutableDictionary alloc]initWithObjectsAndKeys:languageId ,@"language_id",nil];
    [singletone requestPostUrlWithImage:setUrl parameters:parameters image:nil success:^(id  _Nullable responce)
    
        NSArray *objects = [responce allValues];

        NSString *DBPath = [[singletone sharedManager]documentsPathForFileName:@"data.plist"];
        NSError *writeError;
        NSData *plistData = [NSPropertyListSerialization dataWithPropertyList:objects format:NSPropertyListXMLFormat_v1_0 options:NSPropertyListImmutable error:&writeError];
        if(DBPath)
        
            [plistData writeToFile:DBPath atomically:YES];
        
        else
        
            NSLog(@"Error in saveData: %@",writeError);
        

        //Save to bundle
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString *fromPath = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"];
        [fileManager copyItemAtPath:fromPath toPath:DBPath error:nil];

        //Retrive
        NSArray *dtemp = [[NSArray alloc]initWithContentsOfFile:fromPath];
        self.arrayOriginal = [dtemp objectAtIndex:0];
        self.arForTable = [[NSMutableArray alloc] init];
        [self.arForTable addObjectsFromArray:self.arrayOriginal];
        [self.subCatTbl reloadData];
    
    failure:^(NSError * _Nullable error)
    
        NSLog(@"%@",error);
    ];

【问题讨论】:

你的dataStructure是什么样的? 我的意思是 dataStructure 用于存储已解析的 json 数据,您在 tableViewDataSource 中使用该数据 我将 json 响应存储在 plist 文件中 【参考方案1】:

tableViewDelegate中实现如下函数:

- (NSInteger)tableView:(UITableView *)tableView 
indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath  

    id cat = categories[indexPath.item];

    if (cat.sub_sub_cat) 

        return 15; // small indentation
    

    else if (cat.sub_cat) 

        return 0; // no indentation
    

    else 

        return 30; // large indentation
    

注意:我不确定您的dataStructure 是什么样的,或者您是否使用嵌套的tableViews,但这是一般的想法。

【讨论】:

查看我的更新。我正在上传我的 plist 文件的图像。而且我没有使用类似结构的子表视图。我只是在 tableview 中插入新行。我也在上传我的代码以扩展 tableview

以上是关于如何在构建可扩展的tableview时区分json响应数组的主要内容,如果未能解决你的问题,请参考以下文章

Swift - TableView 如何创建可扩展部分

如何在Xamarin.iOS本机平台中扩展tableview单元格?

iphone中的可扩展tableView

iphone中的可扩展tableView

在 TableView 中为使用分页接收的 JSON 数据添加标题标题

如何将 JSON 获取到 Swift 到 tableView 作为部分和行?