4

我有UITableViewController一个NSFetchedResultsController. 每当表格视图为空时,我都会尝试设置从 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;
}

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

我一直在努力寻找以前遇到过类似问题的人,因为我被困在这个问题上好几天了。如果 tableview 加载为空则很好 (1),但是如果 fetchedresultcontroller (2) 包含一个单元格 (3) 并在 (4) 之后删除该单元格,则应用程序崩溃 (5): *** -[UIView isHidden]: message sent to deallocated instance 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:]
4

1 回答 1

2

事实证明,创建一个对 emptyView 具有强引用的属性解决了向已释放实例发送消息的问题。

@interface ListObjectsCoreDataTVC()
    @property (nonatomic, strong) TableViewEmptyBGView* viewForEmptyTable;
@end
于 2013-12-09T13:53:34.877 回答