在一个简单的基于tableview的应用程序中,我有一个addItem按钮,可以打开另一个viewController,我可以在其中添加数据然后保存。问题是单击保存后新记录不会显示在tableView中。我已经尝试使用委托来刷新表,但也无法使用它。我可以看到添加到tableview的新记录的唯一方法是关闭应用程序并重新启动。见下文。
#bookingForm textarea
答案 0 :(得分:1)
这是一个使用协议的例子以及我想要做的事情。
// firstViewController.h
#import "secondViewController.h"
@interface firstViewController : UIViewController <secondViewControllerDelegate>
@end
// firstViewController.m
@implementation firstViewController
// will be called by delegate
- (void)refreshTable
{
// get fresh/updated data from your sqlite before refresh, to get the currently added data...
self.dataSource = [yourFreshDataFromSqlite];
[self.targetTable reloadData];
}
//..
- (void)someInstance
{
secondViewController *svc = [[secondViewController alloc] init];
svc.delegate = self; // this is very important
[self.navigationController pushViewController:svc animated:YES];
}
@end
// secondViewController.h
@protocol secondViewControllerDelegate <NSObject>
- (void)refreshTable;
@end
@interface secondViewController : UIViewController
{
BOOL mustRefresh;
}
@property (weak) id <secondViewControllerDelegate> delegate;
@end
// secondViewController.m
- (void)someOtherInstance
{
mustRefresh = YES; // there is new record
}
- (void) save_Clicked:(id)sender
{
// ..
[self.navigationController dismissViewControllerAnimated:YES completion: ^{
if (mustRefresh) // this also important, checks is you need to reload the table from the firstController
if ([self.delegate respondsToSelector:@selector(refreshTable)]) // this is just for check if 'refreshTable responds', prevents from crashing
[self.delegate refreshTable];
}];
}
这是我得到的日志..
希望我在这里错过了什么..嗯......干杯......