Ios 试图使用searchDisplayController将自定义BackgroundView设置为UITableView,导致消息发送到解除分配的实例

Ios 试图使用searchDisplayController将自定义BackgroundView设置为UITableView,导致消息发送到解除分配的实例,ios,crash,tableview,uisearchdisplaycontroller,Ios,Crash,Tableview,Uisearchdisplaycontroller,我有一个UITableViewController,后面有一个nsfetchedresultscoontroller。 每当tableview为空时,我都会尝试设置从NIB加载的自定义背景视图 - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { NSInteger numberOfSections = [super numberOfSectionsInTableView:tableView];

我有一个
UITableViewController
,后面有一个
nsfetchedresultscoontroller
。 每当tableview为空时,我都会尝试设置从NIB加载的自定义背景视图

- (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;
}
截图:

我一直在努力寻找一个以前有过类似问题的人,因为我已经被困在这个问题上好几天了。 如果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

我现在也有同样的问题。我正在处理的代码用于以编程方式创建tableview,当我将其移动到IB时,它开始给我相同的错误。请看下面我的答案,看看这是否也解决了您的问题。