在UISearchDisplayController上遇到僵尸问题

时间:2013-10-06 21:40:41

标签: storyboard automatic-ref-counting uisearchdisplaycontroller nszombie

使用带有UITableView的UISearchDisplayController时,我有一个僵尸。

UISearchDisplayController(以及视图的其余部分)是通过界面构建​​器设置的(xcode 5上的故事板,仅使用ARC和iOS 7)。

searchDisplayController由这两种方法使用

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    [self filterContentForSearchText:searchString scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    return YES;
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
    [self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
    return YES;
}

乐器正在向我提供此信息

#   Event Type  ∆ RefCt RefCt   Timestamp   Responsible Library Responsible Caller
0   Malloc  +1  1   00:10.739.188   UIKit   -[UITableView setTableHeaderBackgroundColor:]
1   Retain  +1  2   00:10.739.214   UIKit   -[UIView(Internal) _addSubview:positioned:relativeTo:]
2   Retain  +1  3   00:10.739.234   UIKit   -[UISearchDisplayController _configureSearchBarForTableView]
3   Retain  +1  4   00:10.739.291   UIKit   -[UIView(Hierarchy) subviews]
4   Retain  +1  5   00:10.771.238   UIKit   -[UIView(Hierarchy) subviews]
5   Retain  +1  6   00:10.782.890   QuartzCore  -[CALayer layoutSublayers]
6   Release -1  5   00:10.782.891   QuartzCore  -[CALayer layoutSublayers]
7   Release -1  4   00:10.792.538   QuartzCore  CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*)
8   Release -1  3   00:15.446.447   UIKit   -[UITableView dealloc]
9   Release -1  2   00:15.446.661   UIKit   -[UIView(Internal) _invalidateSubviewCache]
10  Release -1  1   00:15.446.679   UIKit   -[UIView(Hierarchy) removeFromSuperview]
11  Release -1  0   00:15.446.744   UIKit   -[UITableView setTableHeaderBackgroundColor:]
12  Zombie      -1  00:15.446.765   UIKit   -[UISearchDisplayController _cleanUpSearchBar]

我试图像这样清理dealloc方法中的UISearchDisplayController

-(void)dealloc {
    self.searchDisplayController.searchResultsTableView.delegate = nil;
    self.searchDisplayController.delegate = nil;
    self.searchDisplayController.searchBar.delegate = nil;
    self.searchDisplayController.searchResultsDelegate = nil;
    self.searchDisplayController.searchResultsDataSource = nil;
}

但它不起作用。

你知道我做错了什么吗?

感谢您的帮助。

3 个答案:

答案 0 :(得分:11)

我想我已经能够解决这个问题了。这是关于解决方法的另一个问题:

UISearchDisplayController causes crash after viewDidUnload

所以我补充道:

@property (nonatomic, weak) IBOutlet UISearchBar *searchBar;
@property (nonatomic) UISearchDisplayController *searchController;

然后在viewDidLoad中:

UISearchDisplayController *searchController = [[UISearchDisplayController alloc] initWithSearchBar:[self searchBar] contentsController:self];
[searchController setDelegate:self];
[searchController setSearchResultsDelegate:self];
[searchController setSearchResultsDataSource:self];
[self setSearchController:searchController];

在dealloc中:

[self setSearchController:nil];

这似乎解决了它。

答案 1 :(得分:1)

可能是一个与searchDisplayController相关的已知问题未被保留。试试这个来增加保留计数:

在.h:

@property (nonatomic, strong) UISearchDisplayController *searchController;

在viewDidLoad中:

self.searchController = (__bridge UISearchDisplayController *)(CFBridgingRetain(self.searchDisplayController));

在dealloc中(不确定是否需要):

self.searchController = CFBridgingRelease((__bridge void*)(self.searchController));

答案 2 :(得分:1)

- (void) viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];

    self.tableView = nil;
}