Tôi đang làm việc trên dự án iPhone trong Xcode 4.3 với SQlite3, kết nối giữa SQlite và Xcode được thực hiện, bây giờ tôi muốn hiển thị dữ liệu của mình (ba lượt xem) và chỉ đọc của nó! vì vậy tôi có chế độ xem bảng chính, chọn raw -> chuyển sang chế độ xem thứ 2 và tải dữ liệu khác từ DB chọn raw -> chuyển đến chế độ xem chi tiết để hiển thị văn bản và hình ảnh dài!Cách hiển thị dữ liệu từ SQlite sang dạng xem bảng sang ứng dụng iPhone
Bất kỳ trợ giúp đánh giá cao.
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
Đây sẽ là một trường hợp hoàn hảo để thiết lập Core Data và sử dụng một NSFetchedResultsController (đây là mục đích chính xác nó được thiết kế cho). Tuy nhiên, có vẻ như bạn đã thực hiện ... ** một cái gì đó ** ... với các thư viện gốc. Vui lòng cụ thể hơn và hiển thị mã cho những gì bạn đã làm. – borrrden
đây là mã của tôi ở trên, không có gì nhiều bạn có thể nhận được từ mã của tôi như tôi vẫn còn mới bắt đầu:/ – baha
Tất cả những gì bạn cần là ở đây ... // Hướng dẫn hoàn hảo cho bạn. [Sqlite with tableview] (http://hi.baidu.com/bailu1234/blog/item/f1a2e3170f6c455ef2de3205.html) – Nit