繁体   English   中英

在目标c中使用嵌套数据填充表视图

[英]Populate table view with nested data in objective c

我嵌套了从服务器获取的数据(NSDictionary)。 我仅使用第一级数据填充表格视图。 现在,我需要通过点击某些单元格来打开某些嵌套数据。 这是数据示例:

treeItem =     (
                {
            name = "smth";
            items = {
                treeItem = {
                    name = "smth";
                    items = {
                        treeItem = {
                            name = "smth";
                            items = {}
                            };
                        },

                        treeItem = {
                            name = "smth";
                            items = {}
                            };
                        };
                    };
                },
                treeItem = {
                    name = "smth";
                    items = {
                        treeItem = {
                            name = "smth";
                            items = {}
                            };
                        };
                    };
                }
            };
        })

如您所见,嵌套的数据(项目)可以不同并且没有限制。 我用键“名称”的值填充了表格视图的单元格。 每个“项目”由“ treeItem”组成,每个都有自己的“名称”。 我需要使用NSDictionary中的“名称”填充单元格。 如果“ items”为空,则什么也不做。 这是我的代码“ MainTableViewController.m”:

#import "MainTableViewController.h"
#import "XMLDictionary.h"
#import "SearchResultsViewController.h"

@interface MainTableViewController () <UISearchResultsUpdating>
@property (strong, nonatomic) NSMutableArray *data;
@property (strong, nonatomic) UISearchController *controller;
@property (strong, nonatomic) NSArray *results;
@end

@implementation MainTableViewController
{
    NSDictionary *xmlDoc;
    NSDictionary *name;

}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self WebServiceSyncCall];

    SearchResultsViewController *searchResults = (SearchResultsViewController *)self.controller.searchResultsController;
    [self addObserver:searchResults forKeyPath:@"results" options:NSKeyValueObservingOptionNew context:nil];

}

- (void) WebServiceSyncCall{
    //Response data object
    NSData *returnData = [[NSData alloc]init];

    //Build the Request
    NSString *param = @"params";

    NSString *postString = [NSString stringWithFormat:@"request=%@",param];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"url"]];

    [request setHTTPMethod:@"POST"];
    [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[postString length]] forHTTPHeaderField:@"Content-length"];
    [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

    //Send the Request
    returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];

    bool debug = YES;

    xmlDoc = [NSDictionary dictionaryWithXMLData:returnData];

    name = [xmlDoc valueForKeyPath:@"TreeItem.name"];

    if (debug && returnData) {

        //            NSLog(@"Response >>>> %@", name);
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSMutableArray *)data {
    if (!_data){
        _data = [[NSMutableArray alloc]init];
        _data=[name mutableCopy];
    }
    return _data;
}

- (UISearchController *)controller{
    if(!_controller){
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        SearchResultsViewController *resultsController = [storyboard instantiateViewControllerWithIdentifier:@"SearchResults"];

        _controller = [[UISearchController alloc]initWithSearchResultsController:resultsController];
        _controller.searchResultsUpdater = self;
    }

    return _controller;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    // Configure the cell...
    cell.textLabel.text = [self.data objectAtIndex:indexPath.row];
    return cell;
}

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

}

#pragma - Search Results Updater

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{

    self.results = nil;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [cd] %@", self.controller.searchBar.text];
    self.results = [self.data filteredArrayUsingPredicate:predicate];
}

- (IBAction)searchButtonPressed:(id)sender {

    [self presentViewController:self.controller animated:YES completion:nil];

}


@end

我能做什么? 任何提示,想法或建议。

要做的想法很简单,只需检查每个键返回的数据。 如果是数组,则表示有字典数组。 如果是字典,则继续读取值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM