重用相同的UITableViewController

时间:2015-04-26 00:55:05

标签: ios objective-c uitableview popviewcontroller

我有一个tableviewcontroller需要向下钻取并显示3层数据。我没有问题向下钻,但当我回去时,细胞和桌子变空了。我目前正在使用故事板,当我使用笔尖时我没有遇到这个问题。所有我必须的是alloc和initWithNibName相同的视图,它将创建相同TableView的另一个实例,我可以返回所有数据将在那里。

我已经尝试过使用segue,但它不能正常工作,因为我需要将tableviewcontroller转换回自身,如果我正在向下钻取。我创建了自己的方法来推送视图控制器并将数据传递给自己的新实例

- (void)drillDown{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    ExerciseTableViewController *tableView = [storyboard instantiateViewControllerWithIdentifier:@"ExerciseTableView"];
    tableView.title = _detailTitle;
    tableView.delegate = self;
    tableView.level = _level;
    tableView.currentSMID = _currentSMID;
    tableView.currentMID = _currentMID;
    tableView.muscleNameArray = _muscleNameArray;
    if (_level == 2) {
        tableView.submuscleNameArray = _submuscleNameArray;
    }
    [self.navigationController pushViewController:tableView animated:YES];
}

这就是我在viewDidLoad中的内容

- (void)viewDidLoad {

[super viewDidLoad];



UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addExercise:)];
self.navigationItem.rightBarButtonItem = add;

NSLog(@"level: %i, currentSMID: %i, currentMID: %i", _level, _currentSMID, _currentMID);

if (self.managedObjectContext_ == nil) {
    self.managedObjectContext_ = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}

if (_level == 1) {
    _submuscleNameArray = [self submuscleGroup:_currentMID valueForKeyPath:@"submuscleGroup"];
    _submuscleIDArray = [self submuscleGroup:_currentMID valueForKeyPath:@"submuscleID"];
} else if (_level == 2) {
    //loading and sorting exercise list
    _exerciseList = [self loadExercise:_currentMID sub:_currentSMID];
    _exerciseListSorted = [self sortKeysByName];
} else {
    [self loadMuscleGroup];
}

}

只有当我在viewDidAppear中放入一些代码时,才会填充表视图,但我无法区分向下钻取和使用viewDidAppear进行备份。

导航控制器是不是要保存当前视图并推送新视图,当我弹出新视图时,我会回到当前视图?

似乎每次我弹出视图时,前一个视图需要重新加载,这导致我以前的视图变为空白。

有什么建议吗?

附加故事板 Storyboard

1 个答案:

答案 0 :(得分:0)

所以我想出了问题所在。我有一个名为_level的变量,我使用它来跟踪我所处的向下钻取级别。当将此变量传递到下一级时,我已将当前级别替换为下一级别的数字,因此当我回到上一个观点。目前的水平与下一个水平相同。

我通过创建一个名为nextLevel的新变量并将其传递下来解决了这个问题。

我的基本错误。

相关问题