• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 更新 tableView 数据

[复制链接]
菜鸟教程小白 发表于 2022-12-13 04:23:06 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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



Best Answer-推荐答案


在呈现和关闭模式的 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)myModalDidFinishDismissingWithDataYourType *)yourData

@end

@interface MyModalViewController : UIViewController

@property (weak) id <MyModalViewControllerDelegate> delegate;

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

@end

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

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap