在UITableView部分添加子部分

时间:2015-12-29 09:10:13

标签: ios objective-c uitableview

搜索了很多但都徒劳无功。我有NSMutableArray NSMutableDictionary的嵌套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,&#39;值&#39;在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

我知道我必须仅将细分操作为细胞但不确定如何操作。

如何访问<?php include("config.php"); include("header.php"); try { $sql = "SELECT * FROM auditplan"; $stmt = $DB->prepare($sql); $stmt->execute(); $result = $stmt->fetchAll(); } catch (Exception $ex) { echo $ex->getMessage(); } ?> <link rel="stylesheet" href="<?=BASE_URL?>/bootstrap/css/bootstrap.css"> <link rel="stylesheet" href="<?=BASE_URL?>/bootstrap/css/bootstrap.min.css"> <script type="text/javascript" src="<?=BASE_URL?>js/jquery2.0.2.min.js"></script> <script type="text/javascript" src="<?=BASE_URL?>js/jquery.dataTables.js"></script> <script type="text/javascript" src="<?=BASE_URL?>js/jquery.dataTables.min.js"></script> <link href="<?=BASE_URL?>themecss/datatables/dataTables.bootstrap.css" rel="stylesheet"> <script src="<?=BASE_URL?>themejs/plugins/datatables/dataTables.bootstrap.js"></script> <script src="<?=BASE_URL?>themejs/jquery-ui-1.10.3.js"></script> <script src="<?=BASE_URL?>themejs/jquery-ui-1.10.3.min.js"></script> <div class="col-sm-9 col-md-10 main"> <h1 class="page-header"> Audit Plan </h1> <a href="Auditplanentry.php" class="btn btn-primary" >Add New</a> <table class="table table-striped" id="auditplantbl"> <thead> <tr> <th>Audit ID</th> <th>Year</th> <th>Month</th> <th>Status</th> <th>Comments</th> <th>Action</th> </tr> </thead> <tbody> <?php foreach($result as $row){ ?> <tr> <td><?php echo $row['auditid']?></td> <td><?php echo $row['year'] ?></td> <td><?php echo $row['month'] ?></td> <td><?php echo $row['status']?></td> <td><?php echo $row['comment']?></td> </tr> <?php } ?> </tbody> </table> </div> <script type="text/javascript"> $(document).ready(function () { $('#auditplantbl').dataTable({ "bLengthChange": false, }); }); </script> 中的子部分?

请帮忙。我该如何解决和实施?

1 个答案:

答案 0 :(得分: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
    }