gpt4 book ai didi

ios - 更新 tableView 数据

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:45:18 25 4
gpt4 key购买 nike

我在 UIView 中嵌入了一个 UITableView,但我不知道如何更新。当我使用嵌入在 UIView 中的 UITableView 进入此页面时,有一个按钮,按下该按钮会弹出一个 modalForm。有一个 textField,用户可以在其中输入名称,然后按下另一个按钮“创建”对象、关闭 modalForm 并更新应用程序数据。但是,我不确定如何刷新我的 UITableView 数据……有没有办法告诉 UITableView 在 modalForm View 被关闭时刷新?

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

最佳答案

在显示和关闭模态的 View Controller 中,您应该调用 dismissViewControllerAnimated:completion 方法来关闭模态。模态不应该自行消失。您可以使用完成 block 来执行您希望在模态完成关闭时执行的任何代码。示例如下。

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

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

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

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

下面是编写您自己的协议(protocol)并使用它的示例。

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 实现文件中的任何位置,调用您选择的委托(delegate)方法。不过,您应该首先确保您的代表确实响应了选择器。MyModalViewController.m 如果 ( [self.delegate respondsToSelector:@selector(myModalDidFinishDismissing)] ) [self.delegate myModalDidFinishDismissing];

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

MyPresentingViewController.h

@interface MyPresentingViewController : UIViewController <MyModalViewControllerDelegate>

MyPresentingViewController.m

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

关于ios - 更新 tableView 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19105252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com