Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4613340
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:32:02+00:00 2026-05-22T01:32:02+00:00

I’m currently having the following issue in my project: I’m using Core Data to

  • 0

I’m currently having the following issue in my project:

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 “searchResults”. My data model has a “type” 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)! What’s wrong with it? (Above the code you’ll find a bonus question :D).

- (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 <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
        return [sectionInfo numberOfObjects];
    }
}

BONUS: 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:32:03+00:00Added an answer on May 22, 2026 at 1:32 am

    I found the problem: it was a simple mistake! What I was doing there was filtering the same array over and over, so, when it got filtered to nothing the first time it hadn’t any object anymore. So I just solved by changing the if-else statement as follows:

    // if (searching)
    NSPredicate *grammarType;
        if (section == 1) {
            NSMutableArray *intermediateResults = [NSMutableArray arrayWithArray:searchResults];
            grammarType = [NSPredicate predicateWithFormat:@"type == %@", @"Intermediate"];
            [intermediateResults filterUsingPredicate:grammarType];
            return [intermediateResults count];
        } else if (section == 2) {
            NSMutableArray *advancedResults = [NSMutableArray arrayWithArray:searchResults];
            grammarType = [NSPredicate predicateWithFormat:@"type == %@", @"Advanced"];
            [advancedResults filterUsingPredicate:grammarType];
            return [advancedResults count];
        } else {
            NSMutableArray *basicResults = [NSMutableArray arrayWithArray:searchResults];
            grammarType = [NSPredicate predicateWithFormat:@"type == %@", @"Basic"];
            [basicResults filterUsingPredicate:grammarType];
            return [basicResults count];
        }
    

    It was simple, right? 😉
    Anyway, I still don’t have an answer to the second question:

    “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?”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.