ios – 尝试使用searchDisplayController将自定义BackgroundView设置为UITableView,从而将消息发送到解除分配的实例

我有一个由NSFetchedResultsController支持的UITableViewController.

每当tableview为空时,我都会尝试设置从NIB加载的自定义backgroundview.

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {

    NSInteger numberOfSections = [super numberOfSectionsInTableView:tableView];

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

        if (numberOfSections == 0 ||
            (numberOfSections == 1 && [super tableView:tableView numberOfRowsInSection:0] == 0))
            [tableView setBackgroundView:[self viewForEmptyTable]];
        else
            [tableView setBackgroundView:nil];
   }
    return numberOfSections;
}

- (TableViewEmptyBGView*) viewForEmptyTable {

    if (_viewForEmptyTable == nil) {
        NSArray* viewForEmptyTableNIB = [[NSBundle mainBundle] loadNibNamed:@"iPhone-ViewForEmptyTable"
                                                                      owner:nil
                                                                    options:nil];

        _viewForEmptyTable = [viewForEmptyTableNIB firstObject];
        [_viewForEmptyTable setHeader:self.listaVaziaHeaderText];
        [_viewForEmptyTable setBody:self.listaVaziaBodyText];
    }

    return _viewForEmptyTable;
}

截图:https://dl.dropboxusercontent.com/u/628444/Crash-Screenshots.png

我一直试图找到一个无用的人,因为我被困在这几天.
如果tableview被加载为空它很好(1),但是如果fetchedresultcontroller(2)包含一个单元格(3)并在(4)之后移除该应用程序崩溃(5):
*** – [UIView isHidden]:消息发送到解除分配的实例0xc6cf440

它似乎与UISearchDisplayController有关,它试图将消息发送到解除分配的对象:

#   Event Type  ∆ RefCt RefCt   Timestamp   Responsible Library Responsible Caller
    Malloc/Retain/Release (8)           00:09.546.357   UIKit   -[UITableView setTableHeaderBackgroundColor:]
0    Malloc +1  1   00:09.546.357   UIKit   -[UITableView setTableHeaderBackgroundColor:]
1    Retain +1  2   00:09.546.385   UIKit   -[UIView(Internal) _addSubview:positioned:relativeTo:]
2    Retain +1  3   00:09.546.409   UIKit   -[UISearchDisplayController _configureSearchBarForTableView]
3    Retain +1  4   00:09.546.474   UIKit   -[UIView(Hierarchy) subviews]
4    Release    -1  3   00:09.552.790   UIKit   -[UIView(Internal) _invalidateSubviewCache]
11   Release    -1  2   01:22.884.181   UIKit   -[UIView(Hierarchy) removeFromSuperview]
12   Release    -1  1   01:22.884.182   UIKit   -[UITableView setTableHeaderBackgroundColor:]
13   Release    -1  0   01:22.884.182   UIKit   -[UISearchDisplayController _updateTableHeaderBackgroundViewInTableView:amountScrolledUnder:]
    Retain/Release (6)          00:09.563.097   UIKit   -[UIView(Hierarchy) subviews]
5    Retain +1  4   00:09.563.097   UIKit   -[UIView(Hierarchy) subviews]
6    Retain +1  5   00:09.657.977   UIKit   -[UIView(Hierarchy) subviews]
7    Retain +1  6   00:09.808.482   QuartzCore  -[CALayer layoutSublayers]
8    Release    -1  5   00:09.808.483   QuartzCore  -[CALayer layoutSublayers]
9    Release    -1  4   00:09.821.299   QuartzCore  CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*)
10   Release    -1  3   01:22.877.290   UIKit   -[UIView(Internal) _invalidateSubviewCache]
14  Zombie      -1  01:22.884.204   UIKit   -[UISearchDisplayController _updateTableHeaderBackgroundViewInTableView:amountScrolledUnder:]

最佳答案 事实证明,创建一个强引用emptyView的属性解决了发送到解除分配的实例的消息的问题.

@interface ListObjectsCoreDataTVC()
    @property (nonatomic, strong) TableViewEmptyBGView* viewForEmptyTable;
@end
点赞