Note that there are some explanatory texts on larger screens.

plurals
  1. POUITableView -> numberOfRowsInSection not working as I thought…
    primarykey
    data
    text
    <p>I'm currently having the following issue in my project:</p> <p>I'm using Core Data to retrieve data from a SQLite database. I use multiple views to display different portions of the database. I also implemented a searchBar to search the entire database. Since my data can be divided in three major sectors, through a BOOL value I tell the tableView to have 3 sections if the status is “searching”, otherwise the entire fetched results shall be divided opportunely in sections using the default Core Data function. When I perform a search the retrieved data is sorted by type and title and the copied to a NSMutableArray called “<strong>searchResults</strong>”. My data model has a <strong>“type”</strong> attribute which I use to know to which area a given element belongs and I want to use this attribute also to determine how many rows each of the three tableView sections shall have (if the number of elements of a section is 0 the section shall appear empty while the others filled with the matching elements). I'm using the following code to do this, but it's not working at all! as you can see there's an if-else if-else control structure inside my numberOfRowsInSection method, but the result of the operation I do inside each condition is always “null”. What's strange (to me) is that if I do any of those operations outside of the control structure the result is exactly what it is supposed to be! It gives me the right number of rows (of course this is the same for all the sections)! <strong>What's wrong with it?</strong> (Above the code you'll find a bonus question :D).</p> <pre><code>- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (searching) { NSPredicate *grammarType; if (section == 1) { grammarType = [NSPredicate predicateWithFormat:@"type == %@", @"Intermediate"]; [searchResults filterUsingPredicate:grammarType]; return [searchResults count]; } else if (section == 2) { grammarType = [NSPredicate predicateWithFormat:@"type == %@", @"Advanced"]; [searchResults filterUsingPredicate:grammarType]; return [searchResults count]; } else { grammarType = [NSPredicate predicateWithFormat:@"type == %@", @"Basic"]; [searchResults filterUsingPredicate:grammarType]; return [searchResults count]; } //If, instead, I use the following it works! /*grammarType = [NSPredicate predicateWithFormat:@"type == %@", @"Basic"]; [searchResults filterUsingPredicate:grammarType]; return [searchResults count];*/ } else { id &lt;NSFetchedResultsSectionInfo&gt; sectionInfo = [[fetchedResultsController sections] objectAtIndex:section]; return [sectionInfo numberOfObjects]; } } </code></pre> <p><strong>BONUS</strong>: I've noticed that the title headers for the sections, while searching (meaning after I got the search results), don't scroll together with the table as usual, but stay fixed in their places. Instead a copy of them seem to be made and those work the usual way. How is it possible?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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