1

UISearchBarXcode 4.6で使用している以下のコードにaを追加したいと思います。誰か助けてくれませんか?

// 

#import "SocialMasterViewController.h"

#import "SocialDetailViewController.h"

@interface SocialMasterViewController () {
    NSXMLParser *parser;
    NSMutableArray *feeds;
    NSMutableDictionary *item;
    NSMutableString *title;
    NSMutableString *link;
    NSString *element;
    NSArray *filteredStrings;
}
@end

@implementation SocialMasterViewController



-(void)gotosharing {
    UIStoryboard *sharingStoryboard = [UIStoryboard storyboardWithName:@"Sharing" bundle:nil];
    UIViewController *initialSharingVC = [sharingStoryboard instantiateInitialViewController];
    initialSharingVC.modalTransitionStyle = UIModalTransitionStylePartialCurl;
    [self presentViewController:initialSharingVC animated:YES completion:nil];
     }


- (void)awakeFromNib
{
    [super awakeFromNib];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    feeds = [[NSMutableArray alloc] init];
    NSURL *url = [NSURL URLWithString:@"http://www.rssmix.com/u/3735817/rss.xml"
            ];
    parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    [parser setDelegate:self];
    [parser setShouldResolveExternalEntities:NO];
    [parser parse];

    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
    [self.tableView addSubview:refreshControl];

}

- (void)refresh:(UIRefreshControl *)refreshControl {
    [refreshControl endRefreshing];
}


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

#pragma mark - Table View

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"];
    return cell;




}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

    element = elementName;

    if ([element isEqualToString:@"item"]) {

        item    = [[NSMutableDictionary alloc] init];
        title   = [[NSMutableString alloc] init];
        link    = [[NSMutableString alloc] init];

    }

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if ([elementName isEqualToString:@"item"]) {

        [item setObject:title forKey:@"title"];
        [item setObject:link forKey:@"link"];

        [feeds addObject:[item copy]];

    }

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

    if ([element isEqualToString:@"title"]) {
        [title appendString:string];
    } else if ([element isEqualToString:@"link"]) {
        [link appendString:string];
    }

}

- (void)parserDidEndDocument:(NSXMLParser *)parser {

    [self.tableView reloadData];

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"showDetail"]) {

        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSString *string = [feeds[indexPath.row] objectForKey: @"link"];
        [[segue destinationViewController] setUrl:string];

    }
}






- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [self filterURLsWithSearchBar:searchText];
    [self.tableView reloadData];
}

- (void)filterURLsWithSearchBar:(NSString *)searchText
{
    //[filteredStrings removeAllObjects];
    for (NSString *rssUrl in feeds)
    {
        NSComparisonResult result = [rssUrl compare:searchText
                                            options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)
                                              range:[rssUrl rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)]];
        if (result == NSOrderedSame) {
            [self->feeds addObject:filteredStrings];
        }
    }
}

@end

基本的に、検索バーに入力されたときに、NSXMLParser によって検索語に取り込まれた結果をフィルター処理したいと考えています。

皆さんからの助けをお待ちしております。

4

3 に答える 3

0

これを行う

  • パーサーから取得した値を配列に格納します。responseArray
  • 別の配列を使用して、テーブルに表示する値を格納します datasourceArray
  • webvservice が正常な応答を受信した後datasourceArray=responseArray、テーブルをリロードします[tableView reloadData]
  • 検索が始まると、検索からresponseArray結果を読み込み、再度DatasourceArray呼び出しますreloadData
于 2013-06-20T09:21:28.953 に答える
0

textFeildDidBegunEditinguitextFeild のデリゲート メソッドを使用できます

次に、このコードはフィルタリングに役立ちます

    NSMutableArray  *array1=(NSMutableArray*)[txtEditFavoriteColor.text componentsSeparatedByString:@" "];//words from textFeild

      for (NSString *str in array1) 
        {
            NSString *tempStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
            if([tempStr length])
            {
                [arryOfWordsToBeSearched addObject:tempStr]; 

            }
        }



        NSMutableArray *subpredicates = [NSMutableArray array];

        for(NSString *term in arryOfWordsToBeSearched) {
            NSPredicate *p = [NSPredicate predicateWithFormat:@"name contains[cd] %@",term];
            [subpredicates addObject:p];
        }

        NSPredicate *filter = [NSCompoundPredicate andPredicateWithSubpredicates:subpredicates];
//***************************************************************predicate made above******
        result = [[NSMutableArray alloc]initWithArray:[arryOfDummyData filteredArrayUsingPredicate: filter]];//search on data array using predicate
于 2013-06-20T09:22:37.397 に答える