2015-11-04 42 views
1

我有兩個數組。對於區段說A,並在其內部陣列B行。我如何訪問它們以顯示numberOfRowsInSection以及關於要在單元格中顯示的部分的行。如何解析給定的JSON數據在目標c中並顯示在表格單元格

我的JSON如下:

{ 
    "data": { 
     "claims": [{ 
      "id": "64", 
       "first_name": "Alia", 
       "last_name": "Bhatt", 
       "vendorimage": "http://projects.kleward.com/salesaward/beta1.5/public/profileImage/img1445325413.jpeg", 
       "claimlist": [{ 
       "campaign_end_date": "2016-10-31", 
        "days": "362", 
        "promotion_id": "7", 
        "promotion_name": "Promotion 1 Jeans", 
        "promotion_description": " Promotion Description Promotion Description Promotion Description Promotion Description Promotion Description Promotion Description Promotion Description Promotion Description Promotion Description Promotion Description Promotion Descripti", 
        "totalClaim": "1", 
        "totatAmount": "200.00000" 
      }] 
     }, { 
      "id": "65", 
       "first_name": "Life", 
       "last_name": "Style", 
       "vendorimage": "http://projects.kleward.com/salesaward/beta1.5/public/profileImage/img1445331248.jpg", 
       "claimlist": [{ 
       "campaign_end_date": "2016-11-23", 
        "days": "385", 
        "promotion_id": "9", 
        "promotion_name": "Promotion 3 Wrist Watch", 
        "promotion_description": "Promotion 3 Wrist Watch Description", 
        "totalClaim": "1", 
        "totatAmount": "300.00000" 
      }] 
     }, { 
      "id": "76", 
       "first_name": "Snap", 
       "last_name": "Deal", 
       "vendorimage": "http://projects.kleward.com/salesaward/beta1.5/public/profileImage/img1445851551.png", 
       "claimlist": [{ 
       "campaign_end_date": "2016-12-29", 
        "days": "421", 
        "promotion_id": "10", 
        "promotion_name": "Dell Promotion", 
        "promotion_description": "Dell Promotion Description", 
        "totalClaim": "1", 
        "totatAmount": "50.00000" 
      }, { 
       "campaign_end_date": "2016-12-30", 
        "days": "422", 
        "promotion_id": "16", 
        "promotion_name": "Mac Promotion", 
        "promotion_description": "Mac Promotion Description", 
        "totalClaim": "1", 
        "totatAmount": "150.00000" 
      }] 
     }] 
    } 
} 
+0

開始與NSJSONSerialization,然後編寫代碼。這就是其他人所做的。 – gnasher729

回答

0
NSArray *sectionArray = [[dictionary objectForKey:@"data"] objectForKey:@"claims"]; 
    NSInteger sectionAmount = [sectionArray count]; 

    for(NSDictionary *detailDict in sectionArray){ 
     NSInteger detailID = [[detailDict objectForKey:@"id"] integerValue]; 
     NSString *firstName = [detailDict objectForKey:@"first_name"]; 

     //And so on, do stuff here with each detail 
    } 
0

解析的JSON在以下結構

  • 維護包含行計數每個部分的NSArray。
  • 維護包含每個部分的數組的NSDictionary,如Section作爲Key和Array作爲值。

表Decoratio

  • 初始化與容量陣列的陣列的計數並保存行計數每個部分。裝飾行使用部分索引來獲取方法「部分中的行數」中的行數
  • 從NSDictionary傳遞節索引獲取數組以裝飾節中的行。
0

正如我你JSON樣品中得到了Sectionclaimsclaims對象是rows。如果是,那麼每一件事情很簡單:

JSON - 是收入數據

NSDictionary * dict = [JSON objectForKey:@"data"]; 

獲取部分,以在numberOfSection使用

NSArray * sectionTitles = [dict allKeys]; 

部分

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

數數部分中的行

對於indexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // init Cell 

    // Get Section Title from indexPath 
    NSString * sectionTitle = [sectionTitles objectAtIndex:indexPath.section]; 

    // Get Array of object for Section 
    NSArray * objects = [dict objectForKey:sectionTitle]; 

    // Get Object for row 
    NSDictionary * object = [objects objectAtIndex:indexPath.row]; 

    // Use it 
    NSString * objectID = [object objectForKey:@"id"]; 

    // return cell; 
} 

當前部分的

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSString * sectionTitle = [sectionTitles objectAtIndex:section]; 
    NSArray * rows = [dict objectForKey:sectionTitle]; 
    return [rows count]; 
} 

行要顯示段頭

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    return [sectionTitles objectAtIndex:section]; 
} 
+0

我想你忘了在cellForRowAtIndexPath方法中返回單元格。而這段代碼在應用程序中將會在titleForHeaderInSection方法中崩潰。謝謝。 –

+0

@GanganiRoshan我曾經提到過你必須在評論中返回單元格// return cell;'當然與前面提到的你必須初始化單元格'// init Cell'但是感謝評論 – Nazir

相關問題