UITableView-> numberOfRowsInSection 没有像我想象的那样工作ߪ

发布于 2024-11-05 14:21:58 字数 2055 浏览 0 评论 0原文

我目前在我的项目中遇到以下问题:

我正在使用 Core Data 从 SQLite 数据库检索数据。我使用多个视图来显示数据库的不同部分。我还实现了一个 searchBar 来搜索整个数据库。由于我的数据可以分为三个主要部分,因此如果状态为“正在搜索”,我通过一个 BOOL 值告诉 tableView 有 3 个部分,否则应使用默认的 Core Data 函数将整个获取的结果适当地分为几个部分。当我执行搜索时,检索到的数据按类型和标题排序,并复制到名为“searchResults”的 NSMutableArray。我的数据模型有一个 “type” 属性,我用它来知道给定元素属于哪个区域,并且我还想使用此属性来确定三个 tableView 部分中的每一个应有多少行(如果某个部分的元素数量为 0,则该部分应显示为空,而其他部分则填充匹配的元素)。我正在使用以下代码来执行此操作,但它根本不起作用!正如你所看到的,我的 numberOfRowsInSection 方法中有一个 if-else if-else 控制结构,但我在每个条件中执行的操作的结果始终为“null”。 (对我来说)奇怪的是,如果我在控制结构之外执行任何这些操作,结果正是它应该的结果!它给了我正确的行数(当然这对于所有部分都是相同的)! 有什么问题吗?(在代码上方您会发现一个额外问题: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];
    }
}

奖励:我注意到,在搜​​索时(即在获得搜索结果后),各部分的标题标题不会像往常一样与表格一起滚动,而是保持固定在其位置。相反,它们似乎被制作了一个副本,并且它们以通常的方式工作。怎么可能呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

避讳 2024-11-12 14:21:58

不禁注意到您有两个分支:(section == 1)。也许这令人困惑?

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

骄傲 2024-11-12 14:21:58

我发现了问题:这是一个简单的错误!我在那里所做的就是一遍又一遍地过滤同一个数组,因此,当它第一次被过滤为空时,它不再有任何对象了。所以我只是通过更改 if-else 语句来解决,如下所示:

// 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];
    }

这很简单,对吧? ;)
无论如何,我仍然没有第二个问题的答案:

“我注意到这些部分的标题标题在搜索时(意思是在我得到搜索结果之后)不会像往常一样与表格一起滚动,但保持固定在原来的位置。相反,它们似乎被制作了一个副本,并且它们以通常的方式工作。怎么可能?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文