UISearchDisplayController的结果延迟了3-6秒

时间:2014-01-27 11:30:15

标签: ios uisearchbar parse-platform uisearchbardisplaycontrol

当我在搜索中输入字母时,我会得到大约4秒的滞后,并且无法做任何事情。其他一切都很好。 UISearchDisplayController找到我想要的东西,segue效果很好。我不确定它是关于内存还是我的代码中的一些错误。谁能帮我?感谢

我的代码

TableViewController.m

@implementation TableViewController

@synthesize colorsTable;
@synthesize searchBar;
@synthesize searchController;
@synthesize searchResults;

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showDetail"]) {
        {
            DetailViewController *sdvc = (DetailViewController *)[segue destinationViewController];


            if(self.searchDisplayController.active) {
                NSIndexPath *indexPath = [[self tableView] indexPathForSelectedRow];
                PFObject *object = [self.objects objectAtIndex:indexPath.row];

                object = (PFObject *)[[self searchResults]objectAtIndex:[[[[self searchDisplayController]searchResultsTableView]indexPathForSelectedRow]row]];
                sdvc.detailItem = object;

            }   else {

                NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
                PFObject *object = [self.objects objectAtIndex:indexPath.row];
                DetailViewController *detailViewController = [segue destinationViewController];
                detailViewController.detailItem = object;


            }

        } 
    }
}


- (void)viewDidLoad
{
    [super viewDidLoad];

    [self performSelector:@selector(runThisMethod) withObject:nil afterDelay:1.9f];
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];

    self.tableView.tableHeaderView = self.searchBar;

    self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];

    self.searchController.searchResultsDataSource = self;
    self.searchController.searchResultsDelegate = self;
    self.searchController.delegate = self;


    CGPoint offset = CGPointMake(0, self.searchBar.frame.size.height);
    self.tableView.contentOffset = offset;

    self.searchResults = [NSMutableArray array];



-(void)filterResults:(NSString *)searchTerm {

    [self.searchResults removeAllObjects];

    PFQuery *query = [PFQuery queryWithClassName: self.parseClassName];
    [query whereKeyExists:@"MestoName"];  //this is based on whatever query you are trying to accomplish
    [query whereKey:@"MestoName" containsString:searchTerm];

    NSArray *results  = [query findObjects];

    NSLog(@"%@", results);
    NSLog(@"%u", results.count);

    [self.searchResults addObjectsFromArray:results];
}


- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    [self filterResults:searchString];
    return YES;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (tableView == self.tableView) {
        //if (tableView == self.searchDisplayController.searchResultsTableView) {

        return self.objects.count;

    } else {

        return self.searchResults.count;

    }

}
- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
{
    tableView.rowHeight = 90.0f;
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {

    static NSString *uniqueIdentifier = @"colorsCell";

    CustomCell *cell = nil;

    cell = (CustomCell *) [self.tableView dequeueReusableCellWithIdentifier:uniqueIdentifier];


    [cell.imagevieww setImageWithURL:[NSURL URLWithString:[object objectForKey:@"ImageURL"]]
                    placeholderImage:[UIImage imageNamed:@"placeholder.png"]];


    cell.cellTitle.text = [object objectForKey:@"MestoName"];
    cell.cellDescript.text = [object objectForKey:@"MestoSubname"];


    if (!cell) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"colorsCell" owner:nil options:nil];

        for (id currentObject in topLevelObjects)
        {
            if([currentObject isKindOfClass:[CustomCell class]])
            {
                cell = (CustomCell *)currentObject;
                break;
            }
        }
    }


        if (tableView == self.tableView) {
            cell.cellTitle.text = [object objectForKey:@"MestoName"];
        } else {

            PFUser *obj2 = [self.searchResults objectAtIndex:indexPath.row];
            PFQuery *query = [PFQuery queryWithClassName:@"Mesta"];
            PFObject *searchedUser = [query getObjectWithId:obj2.objectId];

            NSString *boottext = [searchedUser objectForKey:@"MestoName"];
            cell.cellTitle.text = boottext;

            NSString *bootsubtext = [searchedUser objectForKey:@"MestoSubname"];
            cell.cellDescript.text = bootsubtext;

            NSString *bootimage = [searchedUser objectForKey:@"ImageURL"];
            [cell.imagevieww setImageWithURL:[NSURL URLWithString:bootimage]];


            NSLog(@"Content: %@", boottext);

        }
            return cell;

        }



@end

1 个答案:

答案 0 :(得分:0)

对于每个人都有同样的问题。我已经有了解决方案。我重写了我的所有代码,它终于工作了。

这是代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {

    NSString *uniqueIdentifier = @"MainCell";
    CustomCell3 *cell = nil;
    cell = (CustomCell3 *) [self.tableView dequeueReusableCellWithIdentifier:uniqueIdentifier];




    if (!cell) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MainCell" owner:nil options:nil];

        for (id currentObject in topLevelObjects)
        {
            if([currentObject isKindOfClass:[CustomCell3 class]])
            {
                cell = (CustomCell3 *)currentObject;
                break;
            }
        }
    }

    if (tableView == self.tableView) {
        cell.MainTitle.text = [object objectForKey:@"CountryTitle"];
        cell.DescriptTitle.text = [object objectForKey:@"DescriptTitle"];
        [cell.FlagTitle setImageWithURL:[NSURL URLWithString:[object objectForKey:@"ImaURL"]]
                       placeholderImage:[UIImage imageNamed:@"placeholder.png"]];


    };

if(tableView == self.searchDisplayController.searchResultsTableView) {

        PFObject *searchedUser = [self.searchResults objectAtIndex:indexPath.row];
        NSString *content = [searchedUser objectForKey:@"CountryTitle"];
        NSString *desco = [searchedUser objectForKey:@"DescriptTitle"];
        cell.DescriptTitle.text = desco;
        cell.MainTitle.text = content;
        NSString *image = [searchedUser objectForKey:@"ImaURL"];

    [cell.FlagTitle setImageWithURL:[NSURL URLWithString:image]
                   placeholderImage:[UIImage imageNamed:@"placeholder.png"]];



        };




        return cell;
    }