searchResultsTableView 显示两个相同的单元格 [英] searchResultsTableView displays two of same cell

查看:30
本文介绍了searchResultsTableView 显示两个相同的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从 Parse 服务器中提取数据的搜索栏.我注意到有时在我打字时,tableView 会显示相同单元格的两倍.

I have a search bar that pulls data from a Parse server. I have noticed that sometimes the tableView will display double of the same cell while I am typing.

如果我继续输入它,它将只显示其中一个单元格.我注意到如果我慢慢输入,只有一个单元格会显示整个时间(应该如此).也许 Parse 查询返回的速度太快以至于 tableView 无法正确重新加载?

If I continue to type the it will then only display one of the cells. I have noticed that if I type slowly only one cell displays the entire time (as it should). Perhaps the Parse queries are coming back too fast for the tableView to reload properly?

如果你们能就这个问题提出任何意见,我们将不胜感激!谢谢!

Any light that you guys could shed on this issue would be greatly appreciated! Thank you!

编辑

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[self.searchResult removeAllObjects];
[self.queryArray removeAllObjects];

NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];

PFQuery *query = [PFUser query];
[query whereKey:@"fullName" containsString:searchText];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        for (PFObject *pfObj in objects)
        {
            NSDictionary *person = [[NSDictionary alloc]
                                    initWithObjects:@[pfObj[@"fullName"], pfObj.objectId]
                                    forKeys:@[@"name", @"objectId"]];
            [self.queryArray addObject:person];
        }

        self.searchResult = [NSMutableArray arrayWithArray: [self.contacts filteredArrayUsingPredicate:resultPredicate]];
        [self.searchResult addObjectsFromArray:self.queryArray];
        NSLog(@"query array %@", self.queryArray);
        NSLog(@"%@", self.searchResult);
        [self.searchDisplayController.searchResultsTableView reloadData];
    } else {
        NSLog(@"%@", error);
    }
}];
[self.searchDisplayController.searchResultsTableView reloadData];
}

推荐答案

在更改文本字段值时使数组为零.我的意思是每次键入时都会调用一个函数,在该函数中您将获得排序的值数组.所以在调用它之前让它为零.因为它也存储来自先前搜索的数据,这就是您两次获得相同数据的原因.我遇到了同样的问题,我花了很长时间才弄明白:D

Make your array nil while you are changing the text field Value. What I mean to say every time you type its going to call a function in which you will get sorted array of values. So before calling it make it nil. Because it stores the data from previous search as well and that's why you get same data twice. I had this same Problem and it took me hell of a time to figure this out :D

这篇关于searchResultsTableView 显示两个相同的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆