簡體   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