开发者

UITableView -> numberOfRowsInSection not working as I thought…

开发者 https://www.devze.com 2023-03-04 20:50 出处:网络
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 imple

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"];
        开发者_如何学C    [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?


Couldn't help noticing you have two branches for: (section == 1). Maybe that's confusing something?


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号