背景
最近在做蓝牙设备项目,当蓝牙外设在第三级页面断开连接时,需要更新全部数据,第一级页面进行了UI刷新,这时出现了如下警告
[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];
}];
但是如果把刷新每一条数据,改为使用reloadData ,则不会出现警告,说明问题出现在刷新单cell上,同时警告的内容说明刷新的cell,并未加载到window上,超出了刷新的范围
解决方案
创建一个UIViewController分类,判断当前页面是否在最外层,如果不是,则不进行刷新
- (BOOL)controllerIsInShow {
return (self.isViewLoaded && self.view.window);
}
记录一下 MARK OVER