我能够导航到表视图但是我的表没有显示任何数据,也没有显示错误,我想将我的数据保存到核心数据并希望将其显示到表视图但是它没有显示任何内容
以下是我的应用程序的一些代码片段 -
点击保存按钮 -
- (IBAction)saveDataButton:(id)sender {
NSManagedObjectContext *context = [self manageobjectcontext];
// NSManagedObject *newemployee = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:context];
if (self.employeeDB) {
[self.employeeDB setValue:self.empName.text forKey:@"name"];
[self.employeeDB setValue:self.empEmail.text forKey:@"email"];
[self.employeeDB setValue:self.empDesignation.text forKey:@"designation"];
// [self.employeeDB setValue:self.uploadImage.imageView forKey:@"image"];
}
else{
NSManagedObject *newemployee = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:context];
[newemployee setValue:self.empName.text forKey:@"name"];
[newemployee setValue:self.empEmail.text forKey:@"email"];
[newemployee setValue:self.empDesignation.text forKey:@"designation"];
}
NSError *error = nil;
if (![context save:&error])
{
NSLog(@"Cant save %@ %@",error,[error localizedDescription]);
}
UIViewController *objOfView = [[UIViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
[self.navigationController pushViewController:objOfView animated:YES];
}
这是我的第二个视图控制器代码,用于在表格视图中显示数据 -
-(NSManagedObjectContext *)manageobjectcontext{
NSManagedObjectContext *context = nil;
_delegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
if ([_delegate respondsToSelector:@selector(persistentContainer)]) {
context = _delegate.persistentContainer.viewContext;
}
return context;
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
NSManagedObjectContext *manageobjectcontext = [self manageobjectcontext];
NSFetchRequest *fetchrequest = [[NSFetchRequest alloc]initWithEntityName:@"Employee"];
self.employeeArray = [[manageobjectcontext executeFetchRequest:fetchrequest error:nil]mutableCopy];
[self.emplyoyeeTableView reloadData];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.employeeArray.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
NSManagedObject *emp = [self.employeeArray objectAtIndex:indexPath.row];
[cell.textLabel setText:[NSString stringWithFormat:@"%@",[emp valueForKey:@"name"]]];
[cell.detailTextLabel setText:[NSString stringWithFormat:@"%@",[emp valueForKey:@"email"]]];
[cell.detailTextLabel setText:[NSString stringWithFormat:@"%@",[emp valueForKey:@"designation"]]];
// [cell.imageView setImage:[emp valueForKey:@"image"]];
return cell;
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
答案 0 :(得分:0)
好像你没有正确地实现和设置你的tableview委托。尝试
@interface YourClass ()<UITableViewDelegate, UITableViewDataSource> {
- (void)viewDidLoad {
[super viewDidLoad];
self.employeeArray=[[NSMutableArray alloc] init]; //do this
tableView.dataSource = self
tableview.delegate = self
// Do any additional setup after loading the view.
}
}
答案 1 :(得分:0)
`- (void)viewDidLoad
{
[super viewDidLoad];
self.employeeArray=[[NSMutableArray alloc] init]; //do this step
// Do any additional setup after loading the view.
}`