2015-12-29 30 views
1

的部分小節搜索了很多,但一切都是徒勞。我有一個嵌套的NSMutableDictionaryNSMutableArray。我想在UITableView的部分內有章節。以下是我的數組:加入的UITableView

<__NSArrayM 0x7ffe4267efb0>(
{ 
    "group_title" = "Seller Information"; 
    "group_values" =  (
       { 
      key = "First Name"; 
      value = test; 
     }, 
       { 
      key = "Last Name"; 
      value = testl; 
     } 
    ); 
}, 
{ 
    "group_title" = "Buyer Information"; 
    "group_values" =  (
       { 
      key = "First Name"; 
      value = Demo1; 
     }, 
       { 
      key = "Last Name"; 
      value = Demo; 
     } 
    ); 
}, 
{ 
    "group_title" = "Transaction Information"; 
    "group_values" =  (
       { 
      key = Status; 
      value = Active; 
     }, 
       { 
      key = "MLS #"; 
      value = "15-284"; 
     }, 
       { 
      key = Address; 
      value = "1101 Fargo Ave"; 
     }, 
       { 
      key = County; 
      value = Dickinson; 
     }, 
       { 
      key = Zipcode; 
      value = 51360; 
     }, 
       { 
      key = Contingencies; 
      value =    (
           { 
        key = "General Inspection"; 
        value =      (
               { 
          key = "Contingency Verbiage"; 
          value = "Inspection Results and balance of this paragraph"; 
         } 
        ); 
       }, 
           { 
        key = "New Construction"; 
        value =      (
               { 
          key = "Contingency Group"; 
          value = "If Required"; 
         }, 
               { 
          key = "Days Until Due"; 
          value = 5; 
         } 
        ); 
       } 
      ); 
     } 
    ); 
} 
) 

如果經過上面的數組,然後group_title是我想要的節數。 key,「值」在group_values是在每個部分的行數。

對於關鍵Contingencies,有嵌套的數據。所以我想顯示它如下:

Transaction Information // section title 
    Contingencies // sub-section title 
    General Inspection // sub-sub-section title 
     Contingency Verbiage //value 
    New Construction // sub-sub-section title 
     Contingency Group //value 
     Days Until Due //value 



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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSMutableArray *arr = [[dictData valueForKey:@"group_values"] objectAtIndex:section]; 
    NSUInteger numberOfRows = arr.count; // For second level section headers 
    for (id row in arr) { 
     if([[row objectForKey:@"value"] isKindOfClass:[NSMutableArray class]]) { 
      numberOfRows += [[row valueForKey:@"value"] count]; 
     } 
    } 
    return numberOfRows; 
} 

我知道我必須作爲單元操作的子部分,但不知道如何做到這一點。

如何訪問cellforrowatindexpath中的子部分?

請幫忙。我如何解決和實施這個?

回答

1

您可以使用委託方法indentationLevelForRowAtIndexPath爲單元格添加縮進級別,因此如果要顯示子部分,則只需返回NSInteger值,例如1,則返回值表示要顯示的指定行的深度它在該部分的分層位置。

到目前爲止例的返回值應該是

Contingencies // sub-section title - 1 
    General Inspection // sub-sub-section title - 2 
     Contingency Verbiage //value - 3 
    New Construction // sub-sub-section title - 2 
     Contingency Group //value - 3 
     Days Until Due //value - 3 

添加樣本代碼。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
     //Set the indentation width, so that each indentation level width will increase by it. 
     cell.indentationWidth = 5; 

     //Show the value from corresponding indexPath 
NSArray *arr = [[dictData valueForKey:@"group_values"] objectAtIndex:indexPath.section]; 
NSMutableArray *items = [NSMutableArray arrayWithArray:arr]; 
// For second level section headers 
    for (id row in arr) { 
     if([[row objectForKey:@"value"] isKindOfClass:[NSMutableArray class]]) { 
      [items addObjectsFromArray:[row valueForKey:@"value"]]; 
     } 
    } 
NSDictionary *item = items[indexPath.row] 
     cell.textLabel.text = item[@"key"]; 

     return cell; 

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

     //Retrive the right indentation for item at indexPath 
NSArray *arr = [[dictData valueForKey:@"group_values"] objectAtIndex:indexPath.section]; 
if (indexPath.row < arr.count) { 
return 0 
} 
// For second level section headers 
return 1 
    } 
+0

謝謝,但是我在'cellForRowAtIndexPath'中爲子部分寫了什麼? – z22

+0

因爲'indentationLevelForRowAtIndexPath'將根據定義的子部分爲單元格提供縮進,所以您不必在'cellForRowAtIndexPath'中執行任何特定操作。 – deoKasuhal

+0

你能告訴我一些示例代碼嗎?我不知道如何將這個包含在我的代碼中。我已經在問題中給出了我的代碼。 – z22