0
我正在創建一個簡單的應用程序,它將數據存儲到sqlite數據庫並從中檢索數據。我能夠存儲數據,並且我還能夠使用所有數據填充UITableView,並在原型單元格中顯示一個字段(名稱)。我現在想要做的是打開一個視圖,以顯示所有的細節。所以我確實設置了一個帶有3個字段的viewController來填充,但我不知道如何在該單元格之間傳輸數據到新視圖。推Uiview並填充數據
這裏是我的代碼:
#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);
}
感謝您的回答。我編輯了我的上面的代碼,但沒有改變。 Dett是Dettagli_Esame類中的NSDictionary – Enzoses
幾乎在那裏,在您的視圖中執行加載方法時,使用字典中的條目填充您的uitextfields。 – timthetoolman
我試了一下,請看看我的Edit2點的源代碼。我試着添加NSIndexPath * myIndexPath = [self.tableView indexPathForSelectedRow]; NSDictionary * itemAtIndex =(NSDictionary *)[dataList objectAtIndex:myIndexPath.row]; nome.text = [itemAtIndex objectForKey:@「nome」]; crediti.text = [itemAtIndex objectForKey:@「crediti」]; voto.text = [itemAtIndex objectForKey:@「voto」];在viewDidLoad中,也在prepareForSegue中,但沒有任何變化 – Enzoses