Pracuję nad projektem iPhone'a w Xcode 4.3 z SQlite3, połączenie między SQlite i Xcode jest zakończone, teraz chcę wyświetlić moje dane w widoku tabeli (trzy widoki) i tylko do odczytu! , więc mam główny widok tabeli, wybierz raw -> take to 2nd view i załaduj inne dane z DB select raw -> take to the details view, aby wyświetlić długi tekst i obraz!Jak wyświetlać dane z SQlite w widoku tabeli do aplikacji iPhone'a
Każda pomoc doceniona.
AppDelegate.h
#import "AppDelegate.h"
#import "MasterViewController.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize navigationController = _navigationController;
- (void)dealloc
{
[_window release];
[_navigationController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"cities.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if (success) {
NSLog(@"we have the database");
} else {
NSLog(@"we have no database");
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"cities.sqlite"];
BOOL moved = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:nil];
if (moved) {
NSLog(@"database copied");
}
}
MasterViewController *masterViewController = [[[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil] autorelease];
self.navigationController = [[[UINavigationController alloc] initWithRootViewController:masterViewController] autorelease];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
MasterViewController.h
#import <UIKit/UIKit.h>
#import <sqlite3.h>
@class DetailViewController;
@interface MasterViewController : UITableViewController {
NSMutableArray *cities;
}
@property (strong, nonatomic) DetailViewController *detailViewController;
@end
MasterViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
students = [[NSMutableArray alloc] init];
countries = [[NSMutableArray alloc] init];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)] autorelease];
self.navigationItem.rightBarButtonItem = addButton;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"cities.sqlite"];
sqlite3 *database;
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = "select * from cities_info";
sqlite3_stmt *compileStatement;
if (sqlite3_prepare_v2(database, sqlStatement, -1, &compileStatement, NULL) == SQLITE_OK) {
while (sqlite3_step(compileStatement) == SQLITE_ROW) {
NSLog(@"one record");
NSString *cityName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compileStatement, 1)];
[cities addObject:cityName];
}
NSLog(@"cities: %@",cities);
}
} else {
NSLog(@"error in database");
}
}
Blockquote
Byłby to idealny przypadek skonfigurować danych Core i użyć NSFetchedResultsController (to jest dokładny cel został zaprojektowany dla). Wydaje się jednak, że już zrobiłeś ... ** coś ** ... z natywnymi bibliotekami. Podaj więcej szczegółów i pokaż kod tego, co zrobiłeś. – borrrden
Oto mój kod powyżej, nic nie można uzyskać z mojego kodu, ponieważ wciąż jestem początkujący:/ – baha
Wszystko, czego potrzebujesz, jest tutaj ... // Idealny samouczek dla ciebie. [Sqlite with tableview] (http://hi.baidu.com/bailu1234/blog/item/f1a2e3170f6c455ef2de3205.html) – Nit