写法一 简单快捷
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
  self.navigationController.navigationBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}
生命周期函数里写两行代码就去 Do something..........
缺点:当二级页面pop回来时 一级页面的导航栏右侧会有黑色的一块很不雅观 如下图
未命名.gif
写法二 换种写法
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
  [self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
缺点 pop顺畅了 但是切换底部Tabbar上的item时 导致隐藏了导航栏的控制器上下跳动 如下图
未命名22.gif
写法三 正确写法
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
  [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
解决了以上两个小问题 暂时未发现缺点
记录TableView reloadData不执行的一个小坑 坑坑哇哇 什么玩意😝
应用场景
控制器中 有个数组属性
重写该数组的set方法 去reloadData 当外界每次给控制器的数组赋值时 刷新控制器中的tableView
TableView的创建通过懒加载创建的写法如下
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, CGRectGetHeight(self.view.frame) - 66 - iPhoneXStateBarHeight - 0) style: UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[WordsCell class] forCellReuseIdentifier:cellID];
        _tableView.backgroundColor = HWColor(246, 246, 246);
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    }
    return _tableView;
}
- (void)setDataArr:(NSArray *)dataArr
{
    _dataArr = dataArr;
    [self.tableView reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
}
发现赋值空数组时cellForRowAtIndexPath 没执行
最后尝试了下改成下边的就好了,创建视图的时候就添加.原因未知,其他地方保持上边的写法并没有问题:
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, CGRectGetHeight(self.view.frame) - 66 - iPhoneXStateBarHeight - 0) style: UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[WordsCell class] forCellReuseIdentifier:cellID];
        _tableView.backgroundColor = HWColor(246, 246, 246);
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
       [self.view addSubView:_tableView];
    }
    return _tableView;
}
鉴于能力有限水平一般 理解有误之处 望不吝指出 深表感谢









网友评论