iOS-如何将搜索栏功能添加到此数组?

iOS-如何将搜索栏功能添加到此数组?,ios,cocoa-touch,Ios,Cocoa Touch,有人能告诉我如何将搜索功能添加到包含rss新闻链接标题的数组中吗?我在下面的代码中使用Xcode 4.6.2中的xml解析器收集了rss新闻链接标题?我基本上想要的是能够在模拟器或应用程序中转到viewcontroller中的搜索栏,并能够搜索填充代码中ns url数组中tableview单元格的不同rss标题 // #import "SocialMasterViewController.h" #import "SocialDetailViewController

有人能告诉我如何将搜索功能添加到包含rss新闻链接标题的数组中吗?我在下面的代码中使用Xcode 4.6.2中的xml解析器收集了rss新闻链接标题?我基本上想要的是能够在模拟器或应用程序中转到viewcontroller中的搜索栏,并能够搜索填充代码中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文件代码


期待您的回复:)

只要您的搜索栏发生更改,您就可以更新表视图的内容。 因此,首先过滤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) 重新加载表数据(需要更改行数和数据源)


filterURLsWithSearchBar:searchText是您定义的函数,因此您还需要添加-(void)filterURLsWithSearchBar:(NSString*)searchText;到您的.h文件
- (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;
}