2015-12-29 3 views
1

Искал много, но все напрасно. У меня есть вложенный NSMutableArray из NSMutableDictionary. Я хочу иметь разделы в разделе 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, 'value' в 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

вам не нужно ничего делать в 'cellForRowAtIndexPath', потому что' indentationLevelForRowAtIndexPath' будет давать отступ ячейке в соответствии с определенным подразделением. – deoKasuhal

+0

Не могли бы вы показать мне пример кода? Я не уверен, как включить это в свой код. Я задал свой код в вопросе. – z22

Смежные вопросы