2014-01-27 75 views
0

當我在我的搜索中輸入字母時,我會遇到大約4秒的延遲並且無法執行任何操作。其他一切工作都很好。 UISearchDisplayController找到我想要的,segue很好。我不確定它是關於內存還是代碼中的一些錯誤。誰能幫我?由於UISearchDisplayController結果被延遲3-6secs

我的代碼

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 
+0

需要更多的信息.​​. – codercat

+0

我想我終於知道原因。當我搜索某些東西時,我得到了每個對象單元格1的API,因此如果我將其添加到搜索字母R中,並找到5個對象,它就會給我6個API。當我放棄取消時,它再次計算我6 API。這可能是它需要這麼長時間的原因。我不知道如何減少。你還有什麼更多的信息? – Milan1111

回答

0

對於每個人,誰都有同樣的問題。我已經有了解決方案。我重寫了我的所有代碼,它終於可以工作了。

Here`s代碼

- (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; 
    }