2016-07-25 71 views
1

如何計算每個部分的行數並填充其數據。以下是數據。每個部分的動態行

<__NSCFArray 0x7fadb3e73a60>(
    { 
     AMID = ""; 
     AMName = "xyz"; 
     records =  (
        { 
       EId = 100; 
       EMName = "abc"; 
       EName = "Ename1"; 
      } 
     ); 
     EGroupId = 001; 
     EGroupName = "SectionName1"; 
     stat = G; 
    }, 

    { 
     AMID = ""; 
     AMName = "zyx"; 
     records =  (
        { 
       EId = 144; 
       EMName = "namename"; 
       EName = "somestring"; 
      }, 
        { 
       EId = 159; 
       EMName = "somestring"; 
       EName = "somestring"; 
      }, 
        { 
       EId = 161; 
       EMName = "somestring"; 
       EName = "somestring"; 
      } 

    ); 
      EGroupId = 86; 
      EGroupName = "SectionName2"; 
      stat = Y; 
     } 
) 

從以上數據,我想在節中的「EGroupName」值並且動態地計數每個部分的numberofrowsinsection(在這種情況下,它的「SectionName1和SectionName2)。 最終輸出應該是這樣的,

**SectionName1** 
    return 1; 
**SectionName2** 
    return 3; 

真的很感謝你的幫助。

編輯

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

    NSString *selectedEngagementGroupId = engagementGroupDataArray[indexPath.section][@"EngagementGroupId"]; 
    NSString *selectedEngagementId = modelItem[@"EId"]; 
    NSLog(@"EGId %@",selectedEngagementGroupId); 
    NSLog(@"EId %@",selectedEngagementId); 

} 

回答

2

的陣列,一旦被轉換成對象將是字典的陣列,其中每個字典包含一個數組,每一個包含一個字符串值,該值可以用作部分名稱。這對於多節表格視圖來說是一個非常好的數據源。

解析JSON後,你就會有一個目標C集合,稱之爲:

NSArray *parsedObject; 

段的號碼是:

parsedObject.count; 

一節在indexPath標題是:

parsedObject[indexPath.section][@"EGroupName"] 

行中的區段數是:

NSArray *sectionArray = parsedObject[section][@"records"]; 
[sectionArray count] 

在給定的indexPath型號產品:

NSArray *sectionArray = parsedObject[indexPath.section][@"records"]; 
sectionArray[indexPath.row]; 

編輯去得更深一些,部分陣列本身是字典的數組,所以......

NSArray *sectionArray = parsedObject[indexPath.section][@"records"]; 
NSDictionary *modelItem = sectionArray[indexPath.row]; 

NSNumber *itemId = modelItem[@"EId"]; 
NSNumber *itemEMName = modelItem[@"EMName"]; 
NSNumber *itemName = modelItem[@"EName"]; 

// if the table cell is a just a vanilla UITableViewCell... 
cell.textLabel.text = itemName; 
cell.detailTextLabel.text = itemEMName; 

再次編輯 請記住,您的所有數據源代碼必須遵循此模式,因此要從數據中獲取段級屬性...

NSString *engagementGroupString = engagementGroupDataArray[section][@"EGroupName"]; 
NSString *engagementManagerString = engagementGroupDataArray[section][@"AMName"]; 

編輯一而再,再 您可能會發現它煩人鍵入所有的東西出每個需要它的時候得到模型項目。如果是這樣,你可以通過創建一個輔助函數像壓縮的東西:現在

- (NSDictionary *)modelItemForIndexPath:(NSIndexPath *):indexPath { 
    NSArray *sectionArray = parsedObject[indexPath.section][@"records"]; 
    return sectionArray[indexPath.row]; 
} 

,例如,當用戶選擇的東西:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *selectedEngagementGroupId = engagementGroupDataArray[indexPath.section][@"EngagementGroupId"]; 
    NSDictionary *modelItem = [self modelItemForIndexPath:indexPath]; 
    NSString *selectedEngagementId = modelItem[@"EId"]; 
    NSLog(@"EGId %@",selectedEngagementGroupId); 
    NSLog(@"EId %@",selectedEngagementId); 
} 
+0

我得到每個部分的正確行數。如何顯示每個行詳細的「EName」作爲標題和「EMName」? – user3310076

+0

@ user3310076 - 從OP中的數據看來,記錄數組是一個字典數組,請參閱編輯以瞭解如何處理這些數據。 – danh

+0

應用程序在滾動表時崩潰。由於未捕獲異常'NSRangeException',原因:' - [__ NSCFArray objectAtIndex:]:index(11)超出界限(11),終止應用程序。我將在答案框中發佈我的代碼。請讓我知道我做錯了什麼。 – user3310076

1

我假設你知道如何來轉換成JSON Objective-C的集合(你會得到一個頂級的數組對象我認爲;但是你不顯示頂級對象,這是不合法的JSON)。

您將需要創建一個NSMutableDictionary屬性來保存您的數據,關鍵將是部分名稱,值將是關聯的records數組。

@property NSMutableDictionary *_sections; 

但是一個複雜的是,字典不維持秩序順序與UITableView非常重要的,所以你需要在一個單獨的陣列,以保存訂單:

@property NSMutableArray *_sectionNames; 

分配他們解碼JSON然後在其中存儲數據的代碼位:

NSArray *arrayFromJson = ...; 
for (NSDictionary *dict in arrayFromJson) { 
    NSString *sectionName = dict[@"EGroupName"]; 
    [_sections setObject:dict[@"records"] forKey:sectionName]; 
    [_sectionNames addObject:sectionName]; 
} 

如果要對節例如,按字母順序排列,然後按照您的喜好簡單分類_sectionNames(爲讀者練習)。

然後是UITableDataSource方法將是這樣的:

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSString *sectioName = _sectionNames[section]; 
    return [_sections[sectionName] count]; 
} 
+0

感謝。但是,我得到「沒有可見的@interface for nsmutabledictionary聲明選擇器addObject」錯誤。我已經更新了從webservice收到的實際數據 – user3310076

+0

@danh謝謝;糾正。 – Droppy

+0

@ user3310076我有錯誤的方法名稱。 – Droppy

相關問題