0

次のコードで Xcode 4.6.2 の xml パーサーを使用して収集した RSS ニュース リンクのタイトルを含む配列に検索機能を追加する方法を教えてください。私が基本的に望んでいるのは、シミュレーターまたはアプリでビューコントローラーの検索バーに移動し、コードにある ns url 配列のテーブルビューセルを埋めるさまざまな rss 見出しを検索できるようにすることだけです.

     // 

    #import "SocialMasterViewController.h"

    #import "SocialDetailViewController.h"

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

    @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];

self.mySearchBar.delegate = self;
self.myTableView.delegate = self;
self.myTableView.dataSource = self;


feeds = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:@"http://rssmix.com/u/3747019/rss.xml"
        ];
parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser setShouldResolveExternalEntities:NO];
[parser parse];


    }



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

    #pragma mark - Table View

    // table view and my data source's and delegate methods......

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



    - (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;

static NSString *CellIdentifier =@"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];

        }
    }


    @end

ところで、これは私のmasterviewcontroller.mファイルコードです

皆さんの反応を楽しみにしています:)

4

1 に答える 1

0

検索バーが変更されるたびに、テーブル ビューの内容を更新する必要があります。したがって、最初に URL 文字列をフィルタリングしてから、データをリロードして tableView を更新します。

1) 検索バーが変化したときに呼び出しを行います。

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

2) 表示したい文字列をフィルタリングします

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

3) テーブル データをリロードします (行数とデータソースを変更する必要があります)。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([self.mySerchBar.text isEqualToString:@""] || self.mySearchBar.text == NULL) {
        return totalStrings.count;
    }
    else {
        return filteredStrings.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
    }

    if ([self.mySearchBar.text isEqualToString:@""]|| self.mySearchBar.text == NULL)
    {
        cell.textLabel.text = [totalStrings objectAtIndex:[indexPath row]];
    }
    else {
        cell.textLabel.text = [filteredStrings objectAtIndex:[indexPath row]];
    }
    return cell;
}
于 2013-06-01T16:13:27.973 に答える