我想要的是,当我向数据库中添加新数据时,应该自动重新加载我使用的表视图。
我的表格视图在MasterViewController中。 UserDetailUI包含用于在数据库中存储数据的表单。
这是我的代码:
MasterViewController.h
#import <UIKit/UIKit.h>
#import "DBManager.h"
#import "UserDetailsUi.h"
@interface MasterViewController : UITableViewController<UITableViewDataSource,UITableViewDelegate,ReloadDataDelegate>
{
UserDetailsUi *userDetail;
DBManager *db;
NSMutableArray *firstName;
NSMutableArray *lastName;
NSMutableArray *profileData;
NSMutableArray *rowId;
}
MasterViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tableView.delegate=self;
self.tableView.dataSource=self;
// Conferming Delegate
userDetail = [self.storyboard instantiateViewControllerWithIdentifier:@"UserDetails"];
// here i got the reference of UserDetailUI
userDetail.delegate=self;
// here i got the reference of masterviewController in delegate property.
db = [DBManager getSharedDatabase];
NSString *createTableQuery = @"create table if not exists USER_MASTER (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, FirstName TEXT, LastName TEXT, ProfilePic TEXT)";
[db createTable:createTableQuery];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
[self downloadDataFromDatabase];
[self.tableView reloadData];
}
-(void)downloadDataFromDatabase{
NSString *selectQuery = @"select * from USER_MASTER";
NSDictionary *data = [db retriveAllData:selectQuery];
int numberOfRows = data.count;
NSArray *keys = [[NSArray alloc]initWithArray:[data allKeys]];
firstName = [[NSMutableArray alloc]init];
lastName = [[NSMutableArray alloc]init];
profileData = [[NSMutableArray alloc]init];
rowId = [[NSMutableArray alloc]init];
for (int counter; counter<numberOfRows; counter++) {
NSDictionary *currentRow = [[NSDictionary alloc]initWithDictionary:[data objectForKey:[keys objectAtIndex:counter]]];
[firstName addObject:[currentRow objectForKey:@"firstname"]];
[lastName addObject:[currentRow objectForKey:@"lastname"]];
[profileData addObject:[currentRow objectForKey:@"ProfilePicture"]];
[rowId addObject:[currentRow objectForKey:@"RowId"]];
}
}
- (void)insertNewObject:(id)sender {
[self performSegueWithIdentifier:@"AddDetails" sender:nil];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDate *object = self.objects[indexPath.row];
[[segue destinationViewController] setDetailItem:object];
}
}
-(void)reloadTableData{
// This is my delegate method
[self.tableView reloadData];
}
UserDetailsUi.h
#import <UIKit/UIKit.h>
#import "DBManager.h"
@protocol ReloadDataDelegate <NSObject>
@required
-(void)reloadTableData;
@end
@interface UserDetailsUi : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate>{
UIImagePickerController *imageChooser;
__weak IBOutlet UIImageView *Profile_Pic;
__weak IBOutlet UITextField *lnameTf;
__weak IBOutlet UITextField *firstNameTf;
}
- (IBAction)addDetailsClicked:(id)sender;
@property (nonatomic,weak) id<ReloadDataDelegate> delegate;
@end
UserDetailsUi.m
#import "UserDetailsUi.h"
@implementation UserDetailsUi
- (IBAction)addDetailsClicked:(id)sender {
DBManager *db = [DBManager getSharedDatabase];
NSString *profileEncoded = [self encodeToBase64String:Profile_Pic.image];
NSString *query = [NSString stringWithFormat:@"INSERT INTO USER_MASTER (FirstName,LastName,ProfilePic) values ('%@','%@','%@');",firstNameTf.text,lnameTf.text,profileEncoded];
[db insert:query];
// delegate method call to reload table view
[self.navigationController popViewControllerAnimated:YES];
// at the time of execution this delegate object remains nil
[_delegate reloadTableData];
}
最佳答案
您需要在prepareForSegue而不是viewDidLoad中设置委托。您在viewDidLoad中创建的详细信息控制器的实例与segue创建的实例不同,这就是您的委托为nil的原因。
关于ios - 自定义代表在调用方法时变为零,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24623650/