我有一个容器视图,当用户按下 UIButton 时,它可以在 tableview 和 mapview 之间切换。det 确定它当前在哪个容器视图中。
- (IBAction)changer:(id)sender{
if(det == TRUE){
TableViewController *viewController2 = [[TableViewController alloc]init];
viewController2.view.frame = self.container.bounds;
[viewController2 willMoveToParentViewController:self];
[self.container addSubview:viewController2.view];
[self addChildViewController:viewController2];
[viewController2 didMoveToParentViewController:self];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.container cache:YES];
[UIView commitAnimations];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 55, 35)];
[button setImage:[UIImage imageNamed:@"mapSwitch.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(changer:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setRightBarButtonItem:back];
det = FALSE;
}
else{
ViewController *viewController1 = [self.storyboard instantiateViewControllerWithIdentifier:@"vc1"];
viewController1.view.frame = self.container.bounds;
[viewController1 willMoveToParentViewController:self];
[self.container addSubview:viewController1.view];
[self addChildViewController:viewController1];
[viewController1 didMoveToParentViewController:self];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.container cache:YES];
[UIView commitAnimations];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 55, 35)];
[button setImage:[UIImage imageNamed:@"listSwitch.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(changer:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setRightBarButtonItem:back];
det = TRUE;
}
}
切换就好了;它首先加载正确填充的表格视图。然后,如果你切换到地图视图,它也很好。但是,当我切换回表格视图并调用
[self.tableView reloadData];
方法,tableview 是空的。基本上我有 2 个信息数组需要在 tableview 上显示,它们在切换回 tableview 后返回正确的信息/内容,但在 reloaddata 后不会出现在 tableview 中。
reloadData 不是重新加载之前创建的 tableview 的最佳方法吗?我怀疑当 changer 方法重新加载 tableview 容器时,发生了一些事情,所以 table view 中没有任何内容。
或者有没有办法“缓存”tableview,所以当你切换回它时,一切都会保留而不是重新加载一切?