0

有人可以告诉我如何将搜索功能添加到包含我使用 Xcode 4.6.2 中的 xml 解析器在以下代码中收集的 rss 新闻链接标题的数组中吗?我基本上想要的是能够在模拟器或应用程序中进入我的视图控制器中的搜索栏,并且能够搜索填充我在代码中的 ns url 数组中的 tableview 单元格的不同 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 回答