【问题标题】:iOS search a collection view fasteriOS 更快地搜索集合视图
【发布时间】:2014-11-22 04:00:28
【问题描述】:

在我的应用程序中,我使用了一个可搜索的集合视图,让用户可以找到存储在 coredata 中的人员记录。在输入每个字符时,我都会运行搜索设置。最初,这很好用,但现在我有大量数据,UI 是滞后的。我想我需要使用另一个线程来做到这一点,但我在多线程方面完全是个菜鸟。

这是我的代码:

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    self.fetchedResultsController = nil;

    NSError *error = nil;
    if (![[self fetchedResultsController]performFetch:&error]) {
        abort();
    }

    [self.collectionView reloadData];
    [self.searchBar becomeFirstResponder];
}

有没有办法加快速度?

编辑:我应该注意到搜索字符串正在馈送到他的谓词中......

【问题讨论】:

    标签: ios multithreading core-data uicollectionview uisearchbar


    【解决方案1】:

    我已经使用THIS 来自数据库的 UITABLEVIEW 方法,它的工作原理很迷人。 你是如何实现的:

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    

    你能告诉我们身高代码吗?

    一些代码示例:

    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
    {
     self.feed.counter = 50;
    
     totalcustomers = [cust getTotalCustomer:@"textSearch" searchText:searchtextString custType:YES employeeId:@"" custId:[module getCurrentOrderCustomerId]];
    
            self.currentSearchTitle = searchText;
    
            [cust setDatabasePath:DELEGATE.databasePath];
    
            self.feed.largeArray = (NSMutableArray *) [cust getSeachedCustomer:searchtextString custId:[module getCustomerId:[NSNumber numberWithInt:0]] action:@"textSearch"];
    
            self.availableCustomers = self.feed.largeArray;
    
            [self.allCustomerTable reloadData];
    
            NSLog(@"%d ",totalcustomers);
    }
    

    并在 UITableviewScroll 上加载接下来的 50 条记录:

    - (void)tableViewOverridedForScrollWithSearch:(UITableView *)tableView
                      didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
     NSArray * newPosts;
    
                newPosts = [self.feed newRowsFrom:indexPath.row andAction:@"textSearch" andSearchString:self.currentSearchTitle];
                NSUInteger newCount = [newPosts count];
    
    
    
                if (newCount) {
    
                    [self.availableCustomers addObjectsFromArray:newPosts];
    
                    [self.allCustomerTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
                    return;
                }
            }
    
    }
    

    并获得下一个 50

    - (NSArray *)newRowsFrom:(NSUInteger)newItem andAction:(NSString *)action andSearchString:(NSString *)searchString{
    
    NSLog(@"Counter 1 Print >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> %d", self.counter);
    
    NSArray *result = [self newRowsFromDatabase:self.counter andAction:action andSearchString:searchString];
    
    self.counter = self.counter + 50;
    NSLog(@"Counter 2 Print >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> %d", self.counter);
    return result;
    

    }

    希望对你有帮助 谢谢

    【讨论】:

    • 我不知道这将如何加快从 UISearchBar 的搜索...我的 cellForItemAtIndexPath 非常标准。创建一个单元格,从 fetchedresultscontroller 获取实体对象,设置单元格属性...我没有高度代码...
    • 如果您看到涉及两件事:1. 搜索和 2.loareload 收集中的数据。所以正如你所说,当数据较少时,它工作正常。所以如果用户输入“a”,只需获取前 50 个数据,更新数据源并重新加载 collectionview。现在当用户滚动时加载下一个 50 个数据。依此类推,我已经在我的应用程序中的“搜索、索引、滚动下方”中实现了这样的功能,它使用一个从数据(搜索数据/没有搜索数据的简单,索引数据)中获取的通用引擎。我将在一段时间内显示代码 sn-p
    【解决方案2】:

    一次获取所有结果并将它们保存在一个数组中。然后在每次调用该方法时从该数组中搜索,而不是从核心数据中获取。

    【讨论】:

    • 谢谢。我从来没有将集合视图或表格视图连接到数组。我会调查的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 2015-03-13
    • 2023-04-04
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多