Note that there are some explanatory texts on larger screens.

plurals
  1. POSearch Display Controller results are coming only from the first section of my UITableView
    text
    copied!<p>I am having a sectioned UITableView with a Search Display Controller. My problem is that during a search, only rows from the first section of the Table appear in results. </p> <p>My code is below:</p> <p>First in the ViewDidLoad I get the sections and I create the array of the search results with a capacity equal to the total count of records in my db table.</p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; FMDatabase *db = [FMDatabase databaseWithPath:[Utility getDatabasePath]]; [db open]; //Loading the sections for the TableView self.sectionsarray =[[NSMutableArray alloc] init]; FMResults *results = [db executeQuery:@"SELECT distinct section FROM articles where order by section"]; while([results next]) { NSString *sectioname = [results stringForColumn:@"section"]; [sectionsarray addObject:sectioname]; } [db close]; //Creating the Array for the search results self.searchResults = [NSMutableArray arrayWithCapacity:[db intForQuery: [NSString stringWithFormat:@"SELECT count(id) as rowscount FROM Articles"]]]; if (self.savedSearchTerm) { [self.searchDisplayController.searchBar setText:savedSearchTerm]; self.savedSearchTerm = nil; } [self.tableView reloadData]; } </code></pre> <p>The code below is responsible for doing the actual search. It looks for the search term in any part of a record called ArticleTags in the array that holds the data for the UITableView.</p> <pre><code>- (void)handleSearchForTerm:(NSString *)searchTerm { // Update the filtered array based on the search text [self.searchResults removeAllObjects]; for (Article *searchplace in masterplacesarray) { NSRange range = [searchplace.articletags rangeOfString:searchTerm options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)]; if (range.location != NSNotFound) { [self.searchResults addObject:searchplace]; } } } </code></pre> <p>The following calculates the number of sections in the table view</p> <pre><code>- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { if (tableView == [[self searchDisplayController] searchResultsTableView]) return 1; else { return [sectionsarray count]; } } </code></pre> <p>And this the number of rows for each section</p> <pre><code>- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (tableView == [[self searchDisplayController] searchResultsTableView]) return [[self searchResults] count]; else { FMDatabase *db = [FMDatabase databaseWithPath:[Utility getDatabasePath]]; [db open]; NSUInteger rowscountforsection = [db intForQuery: [NSString stringWithFormat:@"SELECT count(id) as rowscount FROM Articles where section='%@'",[self.sectionsarray objectAtIndex:section]]]; [db close]; return rowscountforsection; } } </code></pre> <p>And this sets the title of each section of the tableview</p> <pre><code>- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ if (tableView == [[self searchDisplayController] searchResultsTableView]) return @"Search Results"; else return [self.sectionsarray objectAtIndex:section]; } </code></pre> <p>This is where I create the tableview's cells section by section. The masterplacesarray is the array that holds the data for the uitableview.</p> <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"PlacesCell"; UITableViewCell *cell = [self.mainTableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } // Configure the cell... FMDatabase *db = [FMDatabase databaseWithPath:[Utility getDatabasePath]]; [db open]; NSString *sectioname = [self.sectionsarray objectAtIndex:indexPath.section]; self.masterplacesarray = [[NSMutableArray alloc] init]; FMResultSet *resultsdata = [db executeQuery:[NSString stringWithFormat:@"SELECT ArticleTitle, ArticlePreview, id, ArticleTags FROM Articles where section='%@'",sectioname]]; while([resultsdata next]) { Article *articledata = [[Article alloc] init]; articledata.articleid = [resultsdata stringForColumn:@"id"]; articledata.articletitle = [resultsdata stringForColumn:@"ArticleTitle"]; articledata.articlepreview = [resultsdata stringForColumn:@"ArticlePreview"]; articledata.articletags = [resultsdata stringForColumn:@"ArticleTags"]; [masterplacesarray addObject:articledata]; } [db close]; Article *placecell = [self.masterplacesarray objectAtIndex:indexPath.row]; if (tableView == [[self searchDisplayController] searchResultsTableView]) { placecell = [self.searchResults objectAtIndex:indexPath.row]; cell.tag = 666; } else { placecell = [self.masterplacesarray objectAtIndex:indexPath.row]; } UILabel *childIDLabel = (UILabel *)[cell viewWithTag:999]; childIDLabel.text = placecell.articleid; UILabel *titleLabel = (UILabel *)[cell viewWithTag:100]; titleLabel.text = placecell.articletitle; UILabel *previewLabel = (UILabel *)[cell viewWithTag:101]; previewLabel.text = placecell.articlepreview; // UIImageView *imageview = (UIImageView *)[cell viewWithTag:102]; // [imageview setImage:[UIImage imageNamed:peoplecell.articlethumb]]; return cell; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload