2013-06-01 86 views
0

有人可以請告訴我如何將搜索功能添加到包含rss新聞鏈接的標題的數組中,這些鏈接是我在以下代碼中使用Xcode 4.6.2中的xml解析器收集的?所有我基本上想要的是能夠去我的視圖控制器,而在模擬器或應用程序中的搜索欄,並能夠搜索不同的rss標題,它們填充ns url數組中的tableview單元格,我在代碼中有這裏。iOS - 如何將搜索欄功能添加到此陣列中?

 // 

    #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 [email protected]"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文件代碼

期待烏爾響應傢伙:)

回答

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; 
} 
+0

filterURLsWithSearchBar:searchText是您定義的函數,因此您還需要添加 - (void)filterURLsWithSearch Bar:(NSString *)searchText;到你的.h文件 – Gallonallen