2012-03-16 2 views
0

SQLiteでToDoリストを作成したいが、このチュートリアルに従っている:http://klanguedoc.hubpages.com/hub/IOS-5-How-To-Display-SQLite-Data-in-a-UITableView しかし、それは機能していない!シミュレータが実行され、アプリケーションが開きますが、テーブルは空白です。私は何か間違っているのですか?私はSnow Leopardにxcode 4.2を使用しています。UITableViewにSQLiteを設定しない

.sqliteファイルには、文字列テキスト、整数優先度、およびブール値があります。しかし、私は物事をより簡単にするために "テキスト"を実装しました。 numberOfRowsInSection::適切な値を返す

// Title.h 
#import <Foundation/Foundation.h> 
@interface Title : NSObject { 
NSString *text; 
} 

@property(nonatomic,copy) NSString *text; 

@end 

//TitleVC.h 
#import <UIKit/UIKit.h> 
#import <sqlite3.h> 

@interface TitleVC : UITableViewController{ 

NSMutableArray *thelist; 
sqlite3 *db; 

} 

@property (nonatomic,retain) NSMutableArray *thelist; 

-(NSMutableArray *) toDo; 

@end 

//TitleVC.m 
#import "TitleVC.h" 
#import "Title.h" 
#import <sqlite3.h> 

@implementation TitleVC 
@synthesize thelist; 

- (void)viewDidLoad 
{ 
    [self toDo]; 
    [super viewDidLoad]; 
} 



- (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 [self.thelist count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"TitleCell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 

int rowCount = indexPath.row; 

Title *title = [self.thelist objectAtIndex:rowCount]; 
cell.textLabel.text = title.text; 

return cell; 
} 



-(NSMutableArray *) toDo{ 
thelist = [[NSMutableArray alloc] initWithCapacity:10]; 
@try{ 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSString *dbPath = [[[NSBundle mainBundle] resourcePath  ]stringByAppendingPathComponent:@"todo.sqlite"]; 
    BOOL success = [fileManager fileExistsAtPath:dbPath]; 
    if(!success) 
    { 
     NSLog(@"Cannot locate database file '%@'.",dbPath); 
    } 
    if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK)) 
    { 
     NSLog(@"An error has occured: %@",sqlite3_errmsg(db)); 
    } 

    const char *sql = "SELECT * FROM todo"; 
    sqlite3_stmt *sqlStatement; 
    if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK) 
    { 
     NSLog(@"Problem with prepare statement: %@", sqlite3_errmsg(db)); 
    }else{ 
     Title *title = [[Title alloc] init]; 
     while (sqlite3_step(sqlStatement)==SQLITE_ROW){ 
      title.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)]; 
      [thelist addObject:title]; 
     } 
    } 
} 
@catch (NSException *exception) { 
    NSLog(@"Problem with prepare statement: %@", sqlite3_errmsg(db)); 
} 
@finally { 
    return thelist; 
} 
} 

答えて

0

あなたは、標準のUITableViewを使用している場合は、のtableViewあり:

は、ここに私のコードですか? tableViewとは何ですか?cellForRowAtIndexPath:returning?私は両方の場所にブレークポイントを置き、メソッドが呼び出されていることを確認するためにチェックします。

最後に、.xibファイルを使用している場合、UITableViewへの接続はありますか?

+0

私はそれを試してみる – sillyparrot

+0

非常に速い応答をありがとう、ありがとう。しかし、私はxcodeにとって非常に新しいです。ブレークポイントは正しい青い矢印ですか?だから私はリターンを言う行に青い矢印を付けるでしょうか?値は下に表示されるはずですか?私はちょうどそれをしたが、シミュレータはまだ実行され、空であり、私は何も表示されません。 – sillyparrot

+0

ああ、しばらくお待ちください、私はブレークポイントを使用する方法を研究しました – sillyparrot

関連する問題