在NSMutableArray中搜索

时间:2015-03-03 10:10:24

标签: objective-c nsarray uisearchbar

您好我如何在tableView中搜索? 我对NSMutableArray使用XML解析。我想搜索时出错。我想对细胞进行详细搜索。

http://i.hizliresim.com/blvZQj.png

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

}

//  FirstViewController.h
//  
//
//  
//  Copyright (c) 2015 Serkan. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "Haber.h"

@interface FirstViewController : UITableViewController<NSXMLParserDelegate,UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchDisplayDelegate>
{
    NSXMLParser *parser;
    NSMutableArray *haberlistesi;
    NSMutableArray *searchArray;
    Haber *haber;
    __weak IBOutlet UITableView *table;
    NSString *currentElement;
}


@property IBOutlet UISearchBar *SearchBar;



@end


//
//  FirstViewController.m
//  
//
//  
//  Copyright (c) 2015 Serkan. All rights reserved.
//

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController
@synthesize SearchBar;


- (void)viewDidLoad
{
    [super viewDidLoad];

    haberlistesi = [[NSMutableArray alloc] init];
    searchArray = [[NSMutableArray alloc] initWithArray:haberlistesi];







    // Hide the search bar until user scrolls up
    CGRect newBounds = [[self tableView] bounds];
    newBounds.origin.y = newBounds.origin.y + SearchBar.bounds.size.height;
    [[self tableView] setBounds:newBounds];
    // Initialize the filteredCandyArray with a capacity equal to the candyArray's capacity



    // Initialize the refresh control.

    self.refreshControl = [[UIRefreshControl alloc] init];
    self.refreshControl.backgroundColor = [UIColor purpleColor];
    self.refreshControl.tintColor = [UIColor whiteColor];
    [self.refreshControl addTarget:self
                            action:@selector(getXMLData)
                  forControlEvents:UIControlEventValueChanged];

    // Initialize the refresh control.


    [self performSelectorInBackground:@selector(getXMLData) withObject:nil];


    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);

    // Reload the table
    [[self tableView] reloadData];

}




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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{
    return [haberlistesi count];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    Haber *temp = [haberlistesi objectAtIndex:indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mycell"];
    NSString *str =[NSString stringWithFormat:@"%@%@",temp.al,temp.sat];
    cell.textLabel.text = str;
    cell.detailTextLabel.text = temp.baslik;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    //tableView.backgroundColor = [UIColor clearColor]; //tableview arkakısmını transparan yapar.
    //cell.textLabel.text = [searchArray objectAtIndex:indexPath.row];

    return cell;
}


-(void)getXMLData
{
    NSString *strURL = @"http://www.serkanuyanik.com/eksperlerimiz.xml";
    NSURL *url = [NSURL URLWithString:strURL];
    NSData *data = [NSData dataWithContentsOfURL:url];
    NSLog([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);

    haberlistesi = [[NSMutableArray alloc] init];
    parser = [[NSXMLParser alloc] initWithData:data];
    parser.delegate = self;
    [parser parse];

                [self performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    currentElement = elementName;
    if ([elementName isEqualToString:@"record"]) {
        haber = [[Haber alloc] init];

    }

}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if ([currentElement isEqualToString:@"sehir"])
        [haber.al appendString:[string stringByReplacingOccurrencesOfString:@"\n" withString:@""]];
    if ([currentElement isEqualToString:@"tarih"])
        [haber.baslik appendString:[string stringByReplacingOccurrencesOfString:@"\n" withString:@""]];

}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@"record"])
    {
        [haberlistesi addObject:haber];
    }
}

-(void)parserDidEndDocument:(NSXMLParser *)parser
{
    [table reloadData];
}


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

    NSString *address = haber.al;


    NSString *mapString = [NSString stringWithFormat:@"http://maps.apple.com/?=%@", address];
    NSURL *urlMapScheme = [NSURL URLWithString:mapString];

    [[UIApplication sharedApplication] openURL:urlMapScheme];

}


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

{



    [self.tableView reloadData];
}




- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

}



- (void)reloadData
{
        // Reload table data
        [self.tableView reloadData];

        // End the refreshing
        if (self.refreshControl) {

            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"MMM d, h:mm a"];
            NSString *title = [NSString stringWithFormat:@"Son Güncelleme: %@", [formatter stringFromDate:[NSDate date]]];
            NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor]
                                                                        forKey:NSForegroundColorAttributeName];
            NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:title attributes:attrsDictionary];
            self.refreshControl.attributedTitle = attributedTitle;

            [self.refreshControl endRefreshing];
        }
    }







@end

1 个答案:

答案 0 :(得分:0)

您可以使用此代码进行搜索:

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    NSArray * searchResults = [haberlistesi filteredArrayUsingPredicate: [NSPredicate predicateWithFormat: @"al CONTAINS[c] %@ OR baslik CONTAINS[c] %@", searchedString, searchedString]];

   //Use this searchResults array as DataSource for your table view and reload the table view, For instance:
   haberlistesi = [searchResults mutableCopy];

   [self.tableView reloadData];
}

快乐编码.. :)