【问题标题】:IOS 7.1 Xcode RSS feeds SearchBar do not display the search results.IOS 7.1 Xcode RSS feed SearchBar 不显示搜索结果。
【发布时间】:2015-04-08 08:07:49
【问题描述】:

我是新手,正在尝试使用 rss 提要开发一个简单的新闻阅读器应用程序。现在我被搜索栏困住了。问题是它会在 tableview 中加载所有数据,但是当我从搜索栏中搜索时,它不会填充 searchResults。

我正在尝试从标题中过滤搜索结果 这是代码。

    #import "APPMasterViewController.h"

    #import "APPDetailViewController.h"

    @interface APPMasterViewController () {
    NSXMLParser *parser;
    NSMutableArray *feeds;
    NSMutableDictionary *item;
    NSMutableString *title;
    NSMutableString *link;
    NSString *element;

    }
    @property (strong,nonatomic ) NSArray *searchResults;
    @end

    @implementation APPMasterViewController
    @synthesize mynewsFeed;

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

   - (void)viewDidLoad {
    [ super viewDidLoad];

    feeds = [[NSMutableArray alloc] init];
    NSURL *url = [NSURL URLWithString:mynewsFeed];
    parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    [parser setDelegate:self];
    [parser setShouldResolveExternalEntities:NO];
    [parser parse];

    self.searchResults = [[NSArray alloc] init];
   }

      - (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;
     if(tableView == self.searchDisplayController.searchResultsTableView)
    {
        return [self.searchResults count];
    }
    else
    {
        return feeds.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    if( tableView == self.searchDisplayController.searchResultsTableView)
    {
        cell.textLabel.text = [[self.searchResults objectAtIndex:indexPath.row] objectForKey: @"title"];
    }
    else
    {

      cell.textLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"];
    }
    return cell;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([[self searchDisplayController] isActive])
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        [self performSegueWithIdentifier:@"showDetail" sender:cell ];
    }
}

#pragma for searching

-(void)FilterContentForSearchText:(NSString *)searchText scope:(NSString *)scope
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",searchText];
    self.searchResults = [feeds filteredArrayUsingPredicate:predicate];
    NSLog(@"All my array list: %@", self.searchResults);
}


-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self FilterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    return YES;
}

#pragma parser methods

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

    }
    #pragma for Segue

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        UITableViewCell *cell = (UITableViewCell *)sender;
        if ([[self searchDisplayController] isActive])
        {

                NSIndexPath *indexPath = [[[self searchDisplayController] searchResultsTableView] indexPathForCell:cell];
                feeds = [[self searchResults] objectAtIndex:[indexPath row]];
            NSString *string = [feeds[indexPath.row] objectForKey: @"link"];
            [[segue destinationViewController] setUrl:string];

        }

        if ([[segue identifier] isEqualToString:@"showDetail"])
        {

            NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

        NSString *string = [feeds[indexPath.row] objectForKey: @"link"];
        [[segue destinationViewController] setUrl:string];
        }
    }

    @end


NSLOG:::===
015-04-08 13:20:45.742 RSSreader[1117:914955] table content: (
        {
        link = "http://indiatoday.feedsportal.com/c/33614/f/589699/s/45338e70/sc/28/l/0Lindiatoday0Bintoday0Bin0Cstory0Capple0Eindia0Eearns0E0U10Ebillion0Ein0Erevenues0C10C4290A490Bhtml/story01.htm";
        title = "Apple India earns $1 billion in revenues";
    },
        {
        link = "http://indiatoday.feedsportal.com/c/33614/f/589699/s/45338e6d/sc/7/l/0Lindiatoday0Bintoday0Bin0Cstory0Cpadma0Evibhushan0Econferred0Eon0Eamitabh0Ebachchan0C10C4290A470Bhtml/story01.htm";
        title = "Big B receives Padma Vibhushan, Abhishek and Aishwarya attend ceremony";
    },
        {
        link = "http://indiatoday.feedsportal.com/c/33614/f/589699/s/45338e74/sc/15/l/0Lindiatoday0Bintoday0Bin0Cstory0C40A0A0A0Eusers0Eto0Etry0Eout0Ethe0Elg0Eg40Eprior0Eto0Ethe0Elaunch0C10C4290A40A0Bhtml/story01.htm";
        title = "4000 users to try out the LG G4 prior to the launch";
    };]
2015-04-08 13:20:49.026 RSSreader[1117:914955] All my array list: (
)

【问题讨论】:

    标签: ios search rss feed


    【解决方案1】:

    你错过了 - [self.tableView reloadData];在 FilterContentForSearchText 方法中

    【讨论】:

    • 我已经尝试过,但没有结果仍然没有更新搜索结果。在我的日志中,谓词凹痕获取结果,并且 searchResults 中没有内容。
    • [[feeds filteredArrayUsingPredicate:predicate] mutableCopy];以此改变。检查返回的内容。
    • 不,即使这样,在搜索栏中输入字符后也不会显示内容。我尝试评论 tableview.reload 仍然没有效果。 :(
    • 是否由于数组提要中的多个值而发生?我怀疑!
    • 将 SELF 更改为 SELF.title
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    • 2014-12-10
    相关资源
    最近更新 更多