penguin-penpen penguin-penpen - 6 months ago 77
iOS Question

iOS - pushViewController:animated: doesn't push a table view properly?

I have a

UITabBarController
and a
HomeTabController
.

What I want to do is to push a
UITableViewController
when I click a button on the view.

However, there did show an animation but no table view presented.

Just to make it clearer, here I have two screenshots.

enter image description here

enter image description here

part of my code



@implementation HomeTabController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.leftBarButtonItem = nil;
self.title = @"SCUxCHG";
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Search"
style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.rightBarButtonItem = rightButton;
self.navigationItem.rightBarButtonItem.target = self;
self.navigationItem.rightBarButtonItem.action = @selector(didClickSearchBtn:);
[self.navigationController setNavigationBarHidden:NO];

_cMainSearchTableViewController = [[HomeTabSearchTableViewController alloc] init];
[self addChildViewController:_cMainSearchTableViewController];

-(IBAction)didClickSearchBtn:(id)sender
{
[self.navigationController setNavigationBarHidden:YES animated:YES];
[self.navigationController pushViewController:_cMainSearchTableViewController animated:YES];
}
@end


MainSearchTableViewController.m

@interface HomeTabSearchTableViewController (){
UISearchController* _cSearchController;
UITableViewController* _cSearchResultTableViewController;
NSArray* _allProductsIdsAndNamesPairs;
NSMutableArray* _filteredProducts;
}
@end

@implementation HomeTabSearchTableViewController


-(void)viewDidLoad{
[super viewDidLoad];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_cSearchController = [[UISearchController alloc] initWithSearchResultsController:_cSearchResultTableViewController];
[self addChildViewController:_cSearchController];
[_cSearchController.searchBar sizeToFit];
self.tableView.tableHeaderView = _cSearchController.searchBar;
[_cSearchController.searchBar setShowsCancelButton:YES];
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view);
make.top.equalTo(self.view);
make.width.mas_equalTo(self.view);
make.height.equalTo(self.view);
}];

_cSearchResultTableViewController.tableView.delegate = self;
_cSearchResultTableViewController.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self getData];
}

- (void)getData{
[ProductModel getAllProductsIdsAndNamesDictionarySuccess:^(BOOL result, NSString* message, NSArray* allProductsIdsAndNamesPaires){
if (!result) {
_allProductsIdsAndNamesPairs = allProductsIdsAndNamesPaires;
NSLog(@"%@", _allProductsIdsAndNamesPairs);
[self.tableView reloadData];
}else{
NSLog(@"%@", message);
}
}failure:^(NSError* error){
}];
}

#pragma mark - UITableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section == 0) {
return 1;
}else{
if (!_cSearchController.active) {
return 0;
}else{
return _filteredProducts.count;
}
}
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString* identifier = @"HomeTabMainSearchTableView";
UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
if (indexPath.section == 0) {
cell.textLabel.text = [NSString stringWithFormat:@"搜索 %@", _cSearchController.searchBar.text];
}else{
cell.textLabel.text = [_filteredProducts objectAtIndex:indexPath.row];
}
return cell;
}

#pragma mark - UISearchResultUpdating

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
[_filteredProducts removeAllObjects];
NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", _cSearchController.searchBar.text];

_filteredProducts = [[_allProductsIdsAndNamesPairs filteredArrayUsingPredicate:searchPredicate] mutableCopy];
dispatch_async(dispatch_get_main_queue(), ^{
[_cSearchResultTableViewController.tableView reloadData];
});
}
@end


You can see from the code that I'm implementing a
UISearchController
with a
UITableView
.

What might be the problem?

There was no error message in the console?

Answer

The main problem is that you add your table view vc as the child view controller and then push it into navigation controller. that's wrong. Try remove

  [self addChildViewController:_cMainSearchTableViewController];

from your viewDidLoad method. Also, try to load your tableView from xib, or storyboard, instead of [[alloc] init]. From existing code - you push vc with zero frame. For example (for xib):

HomeTabSearchTableViewController *vc = [[HomeTabSearchTableViewController alloc] initWithNibName:NSStringFromClass([HomeTabSearchTableViewController class]) bundle:nil];

for storyboard:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"View controller storyboard id"];

UPDATE:

if you don't use storyboard or xib, set up the frame of your view after initialisation, and then push into navigation stack.

Hope this help.