简体   繁体   中英

iOS app freezes after UITableView search

I am a beginner to iOS development and am having trouble finding the cause for this issue.

I have a simple app with a Master and DetailView controllers. The Master View contains an UITableView with a SearchController. Selecting any row on the UITableView will transition to the detailview.

The app freezes when I perform the following sequence

  1. Launch app
  2. Pull down the search bar
  3. Enter text
  4. Select a row from the search results
  5. Select back from the detail view

Now the app freezes after the ViewDidLoad method of the MasterView is loaded. I can't find anything in the system.log.

Here's the code from the MasterViewController

- (void)viewWillAppear:(BOOL)animated {
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
    [self.tableView setContentOffset:CGPointMake(0,40)];
    self.tableView.tableHeaderView = self.searchBar;

    // Create and configure the search controller
    self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
    self.searchController.searchResultsDataSource = self;
    self.searchController.searchResultsDelegate = self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *months = @"Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec";
    feeds = [months componentsSeparatedByString:@","];
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
    self.navigationItem.rightBarButtonItem = addButton;
    self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"numberOfRowsInSection");

    if(tableView == self.tableView)
    {
        return feeds.count;
    }

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like %@", self.searchBar.text];
    self.filteredFeeds = [feeds filteredArrayUsingPredicate:predicate];
    return feeds.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }

    cell.textLabel.text = [feeds objectAtIndex:indexPath.row];


    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        NSDate *object = _objects[indexPath.row];
        self.detailViewController.detailItem = object;
    }

    [self performSegueWithIdentifier: @"showDetail" sender: self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSDate *object = _objects[indexPath.row];
        [[segue destinationViewController] setDetailItem:object];
    }
}

I have uploaded the entire project at the location below.

https://www.dropbox.com/s/yok9vngzv143npa/search.zip

Any kind of assistance is appreciated.

I can not able to run your program because version issue of iOS. But i seen your code. I suspect on 1 point on following code,

- (void)viewWillAppear:(BOOL)animated {
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
    [self.tableView setContentOffset:CGPointMake(0,40)];
    self.tableView.tableHeaderView = self.searchBar;

    // Create and configure the search controller
    self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
    self.searchController.searchResultsDataSource = self;
    self.searchController.searchResultsDelegate = self;
}

You create/allocate UISearchBar and UISearchDisplayController again and again while you get back from DetailViewController to MasterViewController, these may be cause of your app freezing. "viewWillAppear" called before the receiver's view is about to be added to a view hierarchy and before any animations are configured for showing the view. "viewDidLoad" called after view controller has loaded its view hierarchy into memory.

I suggest you place your one time allocations view code in viewDidLoad method.

For more information go through this doc https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/viewDidLoad

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM