iOS TableView在关闭模态后重新加载 [英] iOS TableView Reload after dismissing modal

查看:168
本文介绍了iOS TableView在关闭模态后重新加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我列出核心数据条目在表视图。我想允许用户使用以模态提供的详细视图作为表单视图来编辑记录。在编辑记录时,我观察到了特殊的行为。

In my app, I am listing core data entries in a tableview. I want to allow the user to edit records using a detail view presented modally as a form view. I am observing peculiar behavior when editing records.

流程:


  1. 加载tableview与记录。 - 工作

  2. 用户选择要编辑的记录。 - 工作

  3. 用户在作为模态窗体视图显示的视图控制器中编辑记录。 - 工作

  4. 用户保存修改并关闭表单视图。 - 工作

  5. 表视图显示了之前编辑的记录的正确更改。 - 工作

  6. 用户重复步骤2 - 4选择要编辑的其他记录。 - 工作

  7. Tableview显示所有记录的正确数据。 - 不工作。

  1. User loads tableview with records. -working
  2. User selects a record for editing. -working
  3. User edits record in a view controller presented as a modal form view. -working
  4. User saves edits and dismisses form view. -working
  5. Tableview shows correct changes for the previously edited record. -working
  6. User repeats steps 2 - 4 selecting a different record to edit. -working
  7. Tableview shows correct data for all records. -Not Working.

在第7步,tableview会将第一原始状态。后续记录编辑将导致所有先前的编辑都恢复到其原始状态。如果表视图被关闭并重新加载,则记录是正确的,显示所有编辑。

At step 7, the tableview reverts the display of the first edited record to its original state. Subsequent record edits result in all previous edits reverting to their original state. If the tableview is dismissed and reloaded, the records are correct, showing all the edits.

我在tableView的ViewWillAppear方法中使用了[tableview reload],但是当模态窗体视图控制器关闭时,它似乎不会被触发。

I have used [tableview reload] in the tableview's ViewWillAppear method, but it does not seem to be fired when the modal form view controller is dismissed.

在我的tableview控制器代码中:

In my tableviewcontroller code:

-(void)viewWillAppear:(BOOL)animated
{
    [self.tableView reloadData];
}

搜索周围,我没有找到解决方案,希望有人能指点我

Searching around, I have not found a solution and am hoping someone can point me in the right direction.

谢谢!

推荐答案

一个模态视图,主视图控制器的视图从不消失。因此,在您关闭模态视图后,将不会调用viewWillAppear()。

When you presenting a modal view, main view controller's view never disappears. So, after you dismiss your modal view, viewWillAppear() won't be called.

您可以尝试在模态视图中实现自定义委托函数,在主视图控制器中,当您的数据更新时,触发代理函数重新加载您的tableView,这是在您的主viewController。

You could try to implement a custom delegate function in your modal view, and set it in the main view controller, when your data is updated, fire the delegate function to reload your tableView, which is at your main viewController.

了解什么是代理函数在iOS ,

Understand what is delegate function in iOS, and

如何创建委托函数如下所示:

how to create a delegate function is like this:


  1. p>在您的ModalView.h中:
  1. In your ModalView.h:

// define the protocol for the delegate
@protocol ModelViewDelegate <NSObject> 
  - (void) didUpdateData;
@end

@interface ModalView: ViewController {
     //create a delegate instance
     id delegate;
}

// define delegate instance
@property (nonatomic, assign) id <ModelViewDelegate> delegate;


  • 在您的modalView.m中:

  • In your modalView.m:

    @synthesize delegate;
    

    然后在你的函数中,将委​​托函数放在你需要激活的地方,例如:

    and then inside your function, put the delegate function at place where you need to fire, for example:

    - (void) updateDataIntoDatabase{
           ....
           //Update work done.
    
           [self.delegate didUpdateData];
           //dismiss your modalView;
    }
    


  • MainViewController.h,

    So, in your MainViewController.h,

    #import ModalView.h
    

    @interface ModalView: ViewController <ModelViewDelegate > {
          ...
    }
    

    在MainViewController.m得到一个警告,说你需要实现你已经声明的委托函数。所以,设置委托函数,并做你喜欢做的事情:

    Inside your MainViewController.m, you will get a warning saying that you need to implement the delegate function which you already declared. So, set the delegate function, and do the things that you like to do:

    - (void) didUpdateData{
        [self.tableView reloadData];
    }
    

    在实例化modalView后,不要忘记将modelView委托设置为self实例。如果不是你的委托函数不会被触发。

    DON'T forget to set your modelView delegate to self after you instantiate the modalView instance. If not your delegate function won't be fired.

    modalView.delegate = self;
    

    这篇关于iOS TableView在关闭模态后重新加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆