0

I created an RSS reader that parses from a .xml file. I am trying to create a search bar and search display controller, but am not sure how to search the objectForKey "title" or objectForKey "summary" within the UITableView.

Any help would be greatly appreciated.

The numberOfRowsInSection and cellForRowAtIndexPath looked like this:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.parseResults.count;
}

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

    //Check if cell is nil. If it is create a new instance of it
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    // Configure titleLabel
    cell.textLabel.text = [[self.parseResults objectAtIndex:indexPath.row] objectForKey:@"title"];
    cell.textLabel.numberOfLines = 2;
    //Configure detailTitleLabel
     cell.detailTextLabel.text = [[self.parseResults objectAtIndex:indexPath.row] objectForKey:@"summary"];

    cell.detailTextLabel.numberOfLines = 2;

    //Set accessoryType
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    //Set font and style
     cell.selectionStyle = UITableViewCellSelectionStyleGray;
     cell.textLabel.font = [UIFont boldSystemFontOfSize:14];

    return cell;
}

I recently tried to follow this sample project - https://github.com/deepthit/TableViewSearch.git - based on a suggestion.
My code then looked like this:

@interface QldRecentJudgmentsViewController () {

    __strong NSArray *mFilteredArray_;
    __strong UISearchBar *mSearchBar_;
    __strong UISearchDisplayController *mSearchDisplayController_;
}

@end

 @implementation ViewController
 @synthesize parseResults = _parseResults, HUD;


- (void)viewDidLoad {
    [super viewDidLoad];


    mSearchBar_ = [[UISearchBar alloc] initWithFrame:CGRectMake(0,
                                                            0,
                                                            self.view.bounds.size.width,
                                                            44)];

     mSearchBar_.delegate = self;
     mSearchBar_.placeholder = @"search";
     self.tableView.tableHeaderView = mSearchBar_;

     mSearchDisplayController_ = [[UISearchDisplayController alloc] initWithSearchBar:mSearchBar_
                                                              contentsController:self];
     mSearchDisplayController_.searchResultsDelegate = self;
     mSearchDisplayController_.searchResultsDataSource = self;
     mSearchDisplayController_.delegate = self;

 }



#pragma mark - Table view data source

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
 }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    //return self.parseResults.count;

    if (tableView == self.searchDisplayController.searchResultsTableView ||
    [mFilteredArray_ count] > 0)
    {
        return [mFilteredArray_ count];
    }
    return parseResults.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    id result;
    if (tableView == self.searchDisplayController.searchResultsTableView ||
    [mFilteredArray_ count] > 0)
    {
        result = [mFilteredArray_ objectAtIndex:indexPath.row];
    }
    else
    {
        result = [parseResults objectAtIndex:indexPath.row];
    }


    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    //Check if cell is nil. If it is create a new instance of it
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }
    // Configure titleLabel
    cell.textLabel.text = [[self.parseResults objectAtIndex:indexPath.row] objectForKey:@"title"];
    cell.textLabel.numberOfLines = 2;
    //Configure detailTitleLabel
    cell.detailTextLabel.text = [[self.parseResults objectAtIndex:indexPath.row] objectForKey:@"summary"];

    cell.detailTextLabel.numberOfLines = 2;

    //Set accessoryType
    //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    //Set font and style
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    cell.textLabel.font = [UIFont boldSystemFontOfSize:14];

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *url = [[self.parseResults objectAtIndex:indexPath.row] objectForKey:@"link"];
    NSString *title = [[self.parseResults objectAtIndex:indexPath.row] objectForKey:@"title"];
    WebViewController *viewController = [[WebViewController alloc] initWithURL:url title:title];
    [self.navigationController pushViewController:viewController animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

#pragma mark - UISearchBarDelegate

- (void)searchBar:(UISearchBar *)searchBar
textDidChange:(NSString *)searchText {
    if ([searchText length] == 0)
    {
        [self.tableView reloadData];
        return;
    }

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.title contains[cd] %@ OR SELF.summary contains[cd] %@", searchText, searchText];
    mFilteredArray_ = [self.parseResults filteredArrayUsingPredicate:predicate];

    [self.tableView reloadData];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    mFilteredArray_ = nil;
    [self.tableView reloadData];
}

However, when I follow this the RSS feed does not load anymore in the tableview, so there are no results. Nevertheless when I try to search it does not correctly search the "title" or "summary" and the search results do not appear correctly -the cells are not neatly aligned after searching for something and getting results. Also, the only way to see RSS in the tableview is to search for any generic string, but once you press cancel in the search bar the RSS feed disappears and shows an empty tableview.

Thanks for any help in advance.

Bobi
  • 437
  • 1
  • 8
  • 20
  • Side note - there is no need to use `__strong` with the ivars. That is the default. – rmaddy Nov 21 '13 at 21:16
  • @rmaddy Thanks. This issue has been racking my mind. Using the above code I tried, which still did not resolve the issue should work. I can't seem to fix this issue. – Bobi Nov 21 '13 at 21:29
  • http://www.appcoda.com/how-to-add-search-bar-uitableview/ hope this link will help you. – Ramdhas Nov 22 '13 at 06:11

0 Answers0