我正在创建一个简单的应用程序,将数据存储到sqlite数据库并从中检索数据。我能够存储数据,并且我还能够使用所有数据填充UITableView,在原型单元格中显示字段(名称)。我现在要做的就是打开一个视图,以显示所有细节。所以我确实设置了一个包含3个字段的viewController,但我不知道如何在该单元格之间传输数据到新视图。
这是我的代码:
#import "RootViewController.h"
#import "AppDelegate.h"
#import "Inserimento_Esame.h"
#import "Dettagli_Esame.h"
@interface RootViewController ()
@end
@implementation RootViewController
@synthesize nome,crediti,voto;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *docsDir;
NSArray *dirPaths;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,
YES);
docsDir = [dirPaths objectAtIndex:0];
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:
@"Esami.sqlite"]];
dataList = [[Data alloc] init:databasePath];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
{return YES;}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [dataList getSize];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
}
NSDictionary *itemAtIndex = (NSDictionary *)[dataList objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.text = [itemAtIndex objectForKey:@"nome"];
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.text = [itemAtIndex objectForKey:@"voto"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath {
[self performSegueWithIdentifier:@"DETTAGLI" sender:indexPath];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"DETTAGLI"]) {
NSLog(@"Dettagli");
Dettagli_Esame *destination = [segue destinationViewController];
NSIndexPath * myIndexPath = [self.tableView indexPathForSelectedRow];
NSDictionary *itemAtIndex = (NSDictionary *)[dataList objectAtIndex:myIndexPath.row];
destination.Dett = itemAtIndex;
// I THINK I MUST PUT HERE MY MISSING CODE
}
}
@end
EDIT3
#import "Dettagli_Esame.h"
@interface Dettagli_Esame ()
@end
@implementation Dettagli_Esame
@synthesize nome;
@synthesize crediti;
@synthesize voto;
@synthesize Dett;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{[super viewDidLoad];}
EDIT5-6:
-(void)viewDidAppear:(BOOL)animated{
NSString *docsDir;
NSArray *dirPaths;
NSLog(@"Dettagli Esame");
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,
YES);
docsDir = [dirPaths objectAtIndex:0];
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:
@"Esami.sqlite"]];
dataList = [[Data alloc] init:databasePath];
nome.text = [Dett objectForKey:@"nome"];
crediti.text = [Dett objectForKey:@"crediti"];
voto.text = [Dett objectForKey:@"voto"];
NSLog(@"Dettagli Esame: %@", self.Dett);
}
答案 0 :(得分:0)
你似乎走在了正确的轨道上。 destination
所需的信息与您用于填充单元格的信息相同(实际上,您说3个字段,但我会假设它们都在同一个字典中),所以:
NSDictionary *itemAtIndex = (NSDictionary *)[dataList objectAtIndex:myIndexPath.row];
然后你需要在NSDictionary
类中创建一个Dettagli_Esame
属性并分配它,例如:
destination.displayDictionary = itemAtIndex;
这些行会指向“缺少代码”的位置。