keyboard stuck when searching in uitableview using uisearchbar…?

末鹿安然 提交于 2019-12-13 06:31:24

问题


I have a uitableview and uisearchbar. I'm binding table with tons of records and give functionality to user can search from that. But many times when data size is large keyboard stuck uptill it reload the table. Is it anyway so I can asynchronously search. It's iOS 4 app.

my uisearchbar textDidChange method is as below

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
if(searchText.length == 0)
{
    isFiltered = FALSE;
}
else
{
    @autoreleasepool {
        isFiltered = true;
        membersMFilterArray = [[NSMutableArray alloc] init];
        int lineCount = [[[membersMArray objectAtIndex:0] valueForKey:@"LineCount"] intValue];
        NSString *membersMArrayValue;
        for (int i=0; i<[membersMArray count]; i++)
        {
            membersMArrayValue = [membersMArray objectAtIndex:i];
            NSString *line;
            for (int j=0; j<lineCount; j++)
            {
                line = [NSString stringWithFormat:@"Line%d",j+1];
                NSRange lineRange = [[membersMArrayValue valueForKey:line] rangeOfString:searchText options:NSCaseInsensitiveSearch];
                if(lineRange.location != NSNotFound)
                {
                    [membersMFilterArray addObject:[membersMArray objectAtIndex:i]];
                    break;
                }
            }
        }
    }
}

[tblMember reloadData];}

my array is as below from which I've search, Actually this is one user data which arrise in one table row and if there is 10000 rows then multiply with this. So above for loop is I think as because of this way.

(
    {
    Line1 = "Ashish";
    Line10 = "Ahmedabad";
    Line11 = "Gujarat";
    Line12 = "";
    Line13 = "India";
    Line14 = "";
    Line15 = "";
    Line16 = "abc@yahoo.com";
    Line17 = "";
    Line18 = "";
    Line19 = "xyz";
    Line2 = "Ahmedabad, Gujarat";
    Line20 = "Jun 04, 2012";
    Line3 = "";
    Line4 = "";
    Line5 = "";
    Line6 = "abc";
    Line7 = "xyz";
    Line8 = "";
    Line9 = "";
    LineCount = 20;
    "Member_id" = GM00018004;
    RowNo = 01;
}

)


回答1:


The short answer is yes. You can perform the search asynchronously, say, with Grand Central Dispatch. You can then notify the datasource of your table view to update.

However, it would be much better to make your search more efficient and achieve acceptable real time updates. How to achieve this, depends on how your data is stored and indexed. For example, if you are using Core Data you would have to tweak your NSFetchedResultsController and maybe construct a new entity with single strings of words occurring in your longer strings to be searched.

Looking at your search code, you have two nested loops - an almost certain recipe for performance degradation. Maybe you should consider some of Apple's automatic iteration mechanisms, (there are still iterations, but behind the scenes) such as with key paths or predicates. For instance, something like:

resultArray = [membersArray filteredArrayUsingPredicate:
   [NSPredicate predicateWithFormat:@"self CONTAINS[cd] %@", searchText]];



回答2:


From your code, i think the two for loops are unnecessary. Use better concept to sort the array items.

Some suggestions from my part to speedup.

Remove the auto-release-pool     
Use one for-loop instead of two

You can check it out the start & end time of execution using NSlog.Discover the time taken process and fix it.

Example:

NSLog(@"loop1 started now")
for (int i=0; i<[membersMArray count]; i++)
        {
            membersMArrayValue = [membersMArray objectAtIndex:i];
            NSString *line;
            for (int j=0; j<lineCount; j++)
            {

                NSLog(@"loop2 started now")
                line = [NSString stringWithFormat:@"Line%d",j+1];
                NSRange lineRange = [[membersMArrayValue valueForKey:line] rangeOfString:searchText options:NSCaseInsensitiveSearch];
                if(lineRange.location != NSNotFound)
                {
                    [membersMFilterArray addObject:[membersMArray objectAtIndex:i]];
                    break;
                }
            }  

         NSLog(@"loop2 ended now")
        }

NSLog(@"loop1 ended now")


来源:https://stackoverflow.com/questions/13304425/keyboard-stuck-when-searching-in-uitableview-using-uisearchbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!