简体   繁体   中英

Populate table view with nested data in objective c

I have nested data(NSDictionary) which I get from the server. I populate my table view with only first level of the data. Now I need open certain nested data by tapping on the certain cell. Here's example of the data:

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

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

As you can see nested data(items) can be different and hasn't limit. I populated cells of the table view with the value of the key "name". Each "items" consists of "treeItem" and each of them has own "name". I need populate cells with the "name" from my NSDictionary. If "items" is empty, it just do nothing. Here's my code "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

What can I do? Any tips, ideas or advices.

The idea to go about is simple, just check what data is returned by each key. If it is array, then means there are array of dictionary. And if it is a dictionary, then go on read the values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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