背景
最近在做藍牙設備項目拜效,當藍牙外設在第三級頁面斷開連接時,需要更新全部數(shù)據各谚,第一級頁面進行了UI刷新紧憾,這時出現(xiàn)了如下警告
[TableView] Warning once only: UITableView was told to layout its visible cells and other contents without being in the view hierarchy (the table view or one of its superviews has not been added to a window). This may cause bugs by forcing views inside the table view to load and perform layout without accurate information (e.g. table view bounds, trait collection, layout margins, safe area insets, etc), and will also cause unnecessary performance overhead due to extra layout passes. Make a symbolic breakpoint at UITableViewAlertForLayoutOutsideViewHierarchy to catch this in the debugger and see what caused this to occur, so you can avoid this action altogether if possible, or defer it until the table view has been added to a window. Table view: <UITableView: 0x10388f800; frame = (0 0; 414 672); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x28037b9c0>; layer = <CALayer: 0x280d1e8e0>; contentOffset: {0, 0}; contentSize: {414, 1190.0200004577637}; adjustedContentInset: {0, 0, 0, 0}; dataSource: <ViewController: 0x102c17500>>
首先排除沒有在主線程刷新UI,因為代碼已經使用了[NSOperationQueue mainQueue]
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self.tableView reloadRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationNone];
}];
但是如果把刷新每一條數(shù)據昌渤,改為使用reloadData ,則不會出現(xiàn)警告赴穗,說明問題出現(xiàn)在刷新單cell上,同時警告的內容說明刷新的cell膀息,并未加載到window上般眉,超出了刷新的范圍
解決方案
創(chuàng)建一個UIViewController分類,判斷當前頁面是否在最外層潜支,如果不是甸赃,則不進行刷新
- (BOOL)controllerIsInShow {
return (self.isViewLoaded && self.view.window);
}
記錄一下 MARK OVER