【问题标题】:Update tableView data更新 tableView 数据
【发布时间】:2013-09-30 22:53:18
【问题描述】:

我在 UIView 中嵌入了一个 UITableView,但我不知道如何更新。当我使用嵌入在 UIView 中的 UITableView 来到这个页面时,有一个按钮在按下时会显示一个 modalForm。有一个文本字段,用户可以在其中输入名称,然后按下另一个按钮来“创建”对象、关闭 modalForm 并更新应用程序数据。但是,我不确定如何刷新我的 UITableView 数据……有没有办法告诉 UITableView 在 modalForm 视图被关闭时刷新?

编辑:我知道我需要发送此消息 [tableView reloadData]; 以刷新,但我想知道我可以把它放在哪里以便在关闭 modalForm 时调用它?

【问题讨论】:

  • -[UITableView reloadData]?或者 KVC,但这是一个更高级的话题。
  • 你还在做这个吗?弄明白了吗?
  • 还没有。我只是通过在导航堆栈上推送一个视图控制器而不是进行模态转换来暂时解决了这个问题,但如果可能的话,我仍然希望能够进行模态演示......
  • 好的,那么做模态演示路线缺少什么?下面的答案有什么遗漏吗?如果您在下面准确地评论我的问题,您需要或没有得到什么,我可以编辑我的答案以获得进一步的帮助。

标签: ios objective-c uitableview


【解决方案1】:

在呈现和关闭模式的视图控制器中,您应该调用dismissViewControllerAnimated:completion 方法来关闭模式。模态不应自行消失。您可以使用完成块来执行您希望在模式完成关闭时执行的任何代码。下面的例子。

[self dismissViewControllerAnimated:YES completion:^{
    //this code here will execute when modal is done being dismissed
    [_tableView reloadData];
}];

当然不要忘记避免在block中强捕获self。

如果您最终让模态框自行关闭,您将需要一个委托方法,以便模态框可以与呈现视图控制器进行通信,或者需要一个由模态发送并由呈现视图控制器捕获的通知,或者您可以在呈现视图控制器中实现viewWillAppear:。每次视图即将出现时都会触发此方法。这意味着第一次以及在模态被解除并即将显示它所呈现的视图之后。

------------------------------------------ --------------------------------------------

以下是编写您自己的协议并使用它的示例。

MyModalViewController.h

@protocol MyModalViewControllerDelegate <NSObject>

//if you don't need to send any data
- (void)myModalDidFinishDismissing;

//if you need to send data
- (void)myModalDidFinishDismissingWithData:(YourType *)yourData

@end

@interface MyModalViewController : UIViewController

@property (weak) id <MyModalViewControllerDelegate> delegate;

//place the rest of your properties and public methods here

@end

无论您想在 MyModalViewController 实现文件中的哪个位置调用您选择的委托方法。不过,您应该首先确保您的委托确实响应了选择器。 MyModalViewController.m if ( [self.delegate respondsToSelector:@selector(myModalDidFinishDismissing)] ) [self.delegate myModalDidFinishDismissing];

在呈现模态的视图控制器中,您需要在头文件中声明您符合协议,您需要将模态的委托设置为视图控制器,并确保您实际实现了您的委托方法打算用。

MyPresentingViewController.h

@interface MyPresentingViewController : UIViewController <MyModalViewControllerDelegate>

MyPresentingViewController.m

myModal.delegate = self;
- (void)myModalDidFinishDismissing {
    //do something
    [tableView reloadData];
}

【讨论】:

  • 呈现视图控制器应该采用什么协议才能进行委托?
  • 另外,阅读文档时,我的印象是 viewWillAppear 从presentedViewController 返回时不会被调用?或者至少不是模态的?
  • 如果您要在呈现的 vc 中采用委托,您将需要在您创建的正在呈现的 vc 中创建该协议。我不确定 viewWillAppear 部分,所以我在回答之前在本地对其进行了实际测试。在 viewWillAppear 中设置一个断点,当我关闭呈现的(模态)vc 时它跳闸了。
  • 呈现视图控制器应该采用什么协议?
  • 当您创建将充当模态的视图控制器类时,您在头文件 (.h) 中定义协议。呈现的 vc 将充当模态 vc 的委托,并将响应其协议中声明的方法。
【解决方案2】:

您可以在模态表单类中拥有一个委托变量。当你关闭你的模态表单时,你可以调用类似 [delegate performSelector:@selector(modalFormClosed)] 的东西,它调用 [tableView reloadData]。

@interface ModalForm : UIViewController
@property (nonatomic, weak) id delegate;
@end

@implementation ModalForm
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.delegate performSelector:@selector(modalFormClosed)];
}
@end

在您使用 ModalForm 的班级中:

myModalForm.delegate = self;

并确保您也有 modalFormClosed 方法:

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

或者您可以在模态表单消失时发送 NSNotification(查看 NSNotificationCenter)。

【讨论】:

  • 当我尝试实现这一点时,我遇到了无法“识别”呈现视图控制器的问题。事实上,当我 NSLog 我的modalForm 的presentingViewController 的值时,它会记录NULL。有什么建议吗?
  • 您可能希望将代码粘贴到问题中。你提供的 vc 当然应该知道是谁在展示它。
【解决方案3】:

我希望我有参考,但几年前我看到 Apple 的一条说明,说模态视图不应该自行消失。相反,您在呈现视图控制器上调用方法。在此方法中,演示者关闭模式视图控制器。通常我会在我的 UIViewControllers 上放置一个类别:

@interface UIViewController (Extension)
-(void)simpleDismissAnimated:(BOOL)animated;
@end
@implementation UIViewController (Extension)
-(void)simpleDismissAnimated:(BOOL)animated {
[self dismissViewControllerAnimated:animated completion:nil];
}

要么将它包含在您的所有视图控制器子类中,要么包含在您的 pch 文件中。

所以让你的模态视图控制器调用:

[self.presentingViewController simpleDismissAnimated:YES];

然后在您的呈现视图控制器中,将 simpleDismissAnimated: 的实现重写为:

-(void)simpleDismissAnimated:(BOOL)animated {
[_tableView reloadData];
[self dismissViewControllerAnimated:animated completion:nil;
}

这应该会解决它。

【讨论】:

    【解决方案4】:

    试试这条线:

     [self.tableView reloadData];
    

    【讨论】:

    • 我想知道一旦我关闭了modalForm,我将如何调用该行?当我关闭 modalForm 时,是否有一种方法会被调用?
    【解决方案5】:

    您可以尝试将[tableView reloadData] 放入 tableView 所在的 viewWillAppear 方法中。

    【讨论】:

      【解决方案6】:

      我遇到了同样的问题。 ReloadData 对我不起作用。我解决了它,所以我做了这样的新方法:

      - (void)insertNewString:(NSString *)text{
              if (!_yourArrayWithData) {
                  _yourArrayWithData = [[NSMutableArray alloc] init];
              }
              [_yourArrayWithData insertObject:text atIndex:0];
              NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
              [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
          }
      

      当我想在 tableView 中添加一些东西时,我会使用具有如下操作的按钮:

      - (IBAction)SaveNewPhrase:(id)sender {        
      NSString *mainText = _NewPhraseTextView.text;   //I take nsstring from textView
      
      if([mainText isEqual:@""]){
          return;
      }else{
          NSMutableArray *contentFile = [NSArray arrayWithContentsOfFile:docPathContents()];
          NSMutableArray *copyContentFile = [[NSMutableArray alloc] init];
      
                //save array with new item        
          [copyContentFile addObject:mainText];
          [copyContentFile addObjectsFromArray:contentFile];
          [copyContentFile writeToFile:docPathContents() atomically:YES];   
      
          [self insertNewString:mainText];      //refresh tableView
      
          [_NewPhraseTextView resignFirstResponder];
      }}
      

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-06
        • 2014-03-06
        相关资源
        最近更新 更多