菜鸟教程小白 发表于 2022-12-13 04:23:06

ios - 更新 tableView 数据


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

<p>编辑:我知道我需要发送此消息 <code>;</code> 以刷新,但我想知道我可以把它放在哪里,以便在解除 modalForm 时调用它? </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在呈现和关闭模式的 ViewController 中,您应该调用 <code>dismissViewControllerAnimated:completion</code> 方法来关闭模式。模态不应自行消失。您可以使用完成 block 来执行您希望在模式完成关闭时执行的任何代码。下面的例子。</p>

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

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

<p>如果您最终让模态自行关闭,您将需要一个委托(delegate)方法,以便模态可以与呈现 ViewController 进行通信,或者由模态发送并由呈现 ViewController 捕获的通知,或者您可以在呈现 ViewController 中实现 <code>viewWillAppear:</code>。每次 View 即将出现时都会触发此方法。这意味着第一次以及在模态被关闭并即将显示它所呈现的 View 之后。</p>

<p><strong>------------------------------------------ --------------------------------------------</strong></strong></p>

<p>以下是编写您自己的协议(protocol)并使用它的示例。</p>

<p>MyModalViewController.h</p>

<pre><code>@protocol MyModalViewControllerDelegate &lt;NSObject&gt;

//if you don&#39;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 &lt;MyModalViewControllerDelegate&gt; delegate;

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

@end
</code></pre>

<p>无论您想在 MyModalViewController 实现文件中的哪个位置调用您选择的委托(delegate)方法。不过,您应该首先确保您的委托(delegate)确实响应了选择器。
MyModalViewController.m
    if ( )
            ;</p>

<p>在呈现模态的 ViewController 中,您需要在头文件中声明您符合协议(protocol),您需要将模态的委托(delegate)设置为 ViewController ,并确保您实际实现了您的委托(delegate)方法打算用。</p>

<p>MyPresentingViewController.h</p>

<pre><code>@interface MyPresentingViewController : UIViewController &lt;MyModalViewControllerDelegate&gt;
</code></pre>

<p>MyPresentingViewController.m</p>

<pre><code>myModal.delegate = self;
- (void)myModalDidFinishDismissing {
    //do something
    ;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 更新 tableView 数据,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/19105252/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/19105252/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 更新 tableView 数据