Question

When I type letters into my Search I get lag for about 4secs and can`t do anything. Everything others work good. UISearchDisplayController find me what I want, segue works good . I am not sure if it is about memory or some mistake in my code. Can anyone help me? Thanks

My code

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
Was it helpful?

Solution

For everyone, who have the same problem. I have got already the solution. I rewrote all my code and it is finally working.

Here`s the code

- (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;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top