iPhone TableView搜索XML [英] iPhone TableView Search XML

查看:124
本文介绍了iPhone TableView搜索XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于在tableview中向搜索栏添加XML的问题。我可以在tableview中加载所有外部XML文件,但是当我点击顶部的搜索栏并点击一封信时,它会崩溃。

I have a question about adding XML to the searchbar in a tableview. I can get all the external XML file to load in the tableview, but when I hit the searchbar up top, and hit a letter, it crashes.

我认为这是某种东西我做错了很简单。在我的RootViewController中,有一个名为searchTableView的函数。我觉得这就是它没有拿起搜索项目的地方。我认为它围绕着objectForKey:@title。当我调试时,我也收到此错误消息:NSCFArray objectForKey无法识别的选择器。这是我的searchTableView函数:

I think it's something really simple that I'm doing wrong. In my RootViewController, there's a function called searchTableView. I feel like that's where it's not picking up the search items. I think it's somewhere around the objectForKey:@"title". When I debug, I get this error message also: "NSCFArray objectForKey unrecognized selector". Here's my searchTableView function:

- (void) searchTableView {

    NSString *searchText = searchBar.text;
    NSMutableArray *searchArray = [[NSMutableArray alloc] init];

    for (NSDictionary *dictionary in listOfItems)
    {
        NSArray *array = [dictionary objectForKey:@"title"];
        [searchArray addObjectsFromArray:array];
    }

    for (NSString *sTemp in searchArray)
    {
        NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (titleResultsRange.length > 0)
            [copyListOfItems addObject:sTemp];
    }

    [searchArray release];
    searchArray = nil;
}


推荐答案

好的想通了。由于某种原因,很难找到如何执行此操作的文档。

Ok figured it out. For some reason this was really hard to find documentation how to do this.

这是我的RootViewController.m。

Here's my RootViewController.m below.

我的pList配置为:

My pList is configured as:


  • 根(数组)

  • Item0(字典)

  • 姓名(字符串)

  • 第1项(字典)

  • 名称(字符串)..

  • Root (Array)
  • Item0 (Dictionary)
  • Name (String)
  • Item1 (Dictionary)
  • Name (String)..

这是我的代码,希望这可以帮助其他人寻求帮助:

Here's my code, hopefully this helps anyone else looking for help on this:

@implementation RootViewController
@synthesize listOfItems, copyListOfItems;

- (void)viewDidLoad {
    [super viewDidLoad];

    //Initialize the array.
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"plistArray" ofType:@"plist"];  
    NSMutableArray* tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
    self.listOfItems = tmpArray;
    [tmpArray release];

    //Initialize the copy array.
    copyListOfItems = [[NSMutableArray alloc] init];

    //Set the title
    self.navigationItem.title = @"Search";

    //Add the search bar
    self.tableView.tableHeaderView = searchBar;
    searchBar.autocorrectionType = UITextAutocorrectionTypeNo;

    searching = NO;
    letUserSelectRow = YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    if (searching)
        return 1;
    else
        return 1;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (searching)
        return [copyListOfItems count];
    else {

        //Number of rows it should expect should be based on the section
        return [listOfItems count];
    }
}


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

    //Get the selected country

    NSString *selectedCountry = nil;

    if(searching)
        selectedCountry = [copyListOfItems objectAtIndex:indexPath.row];
    else {

        // Navigation logic may go here. Create and push another view controller.

    }

    NSDictionary *dictionary = [self.listOfItems objectAtIndex:indexPath.row];
    FoodDetail *dvController = [[FoodDetail alloc] initWithNibName:@"FoodDetail" bundle:nil andDictionary: dictionary];

    // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:dvController animated:YES];
    [dvController release];

}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set up the cell...

    if(searching) 
        cell.textLabel.text = [copyListOfItems objectAtIndex:indexPath.row];
    else {

        cell.textLabel.text = [[self.listOfItems objectAtIndex:indexPath.row] 
                               objectForKey:@"Name"];
    }

    return cell;
}



- (NSIndexPath *)tableView :(UITableView *)theTableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if(letUserSelectRow)
        return indexPath;
    else
        return nil;
}





#pragma mark -
#pragma mark Search Bar 

- (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar {

    //This method is called again when the user clicks back from the detail view.
    //So the overlay is displayed on the results, which is something we do not want to happen.
    if(searching)
        return;

    //Add the overlay view.
    if(ovController == nil)
        ovController = [[OverlayViewController alloc] initWithNibName:@"OverlayView" bundle:[NSBundle mainBundle]];

    CGFloat yaxis = self.navigationController.navigationBar.frame.size.height;
    CGFloat width = self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height;

    //Parameters x = origion on x-axis, y = origon on y-axis.
    CGRect frame = CGRectMake(0, yaxis, width, height);
    ovController.view.frame = frame;    
    ovController.view.backgroundColor = [UIColor grayColor];
    ovController.view.alpha = 0.5;

    ovController.rvController = self;

    [self.tableView insertSubview:ovController.view aboveSubview:self.parentViewController.view];

    searching = YES;
    letUserSelectRow = NO;
    self.tableView.scrollEnabled = NO;

    //Add the done button.
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] 
                                               initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
                                               target:self action:@selector(doneSearching_Clicked:)] autorelease];

}

- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText {

    //Remove all objects first.
    [copyListOfItems removeAllObjects];

    if([searchText length] > 0) {

        [ovController.view removeFromSuperview];
        searching = YES;
        letUserSelectRow = YES;
        self.tableView.scrollEnabled = YES;
        [self searchTableView];
    }
    else {

        [self.tableView insertSubview:ovController.view aboveSubview:self.parentViewController.view];

        searching = NO;
        letUserSelectRow = NO;
        self.tableView.scrollEnabled = NO;
    }

    [self.tableView reloadData];
}

- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar {

    [self searchTableView];
}

- (void) searchTableView {

    NSString *searchText = searchBar.text;
    NSMutableArray *searchArray = [[NSMutableArray alloc] init];

    for (NSDictionary *dictionary in listOfItems)
    {
        NSString *text1 = [dictionary objectForKey:@"Name"];
        [searchArray addObject:text1];
    }   
    NSLog(@"%s: searchArray=%@", __func__, searchArray);

    for (NSString *sTemp in searchArray)
    {
        NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (titleResultsRange.length > 0)
            [copyListOfItems addObject:sTemp];
    }

    [searchArray release];
    searchArray = nil;
}

- (void) doneSearching_Clicked:(id)sender {

    searchBar.text = @"";
    [searchBar resignFirstResponder];

    letUserSelectRow = YES;
    searching = NO;
    self.navigationItem.rightBarButtonItem = nil;
    self.tableView.scrollEnabled = YES;

    [ovController.view removeFromSuperview];
    [ovController release];
    ovController = nil;

    [self.tableView reloadData];
}


- (void)dealloc {

    [ovController release];
    [copyListOfItems release];
    [searchBar release];
    [listOfItems release];
    [super dealloc];
}


@end

这篇关于iPhone TableView搜索XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆