2012-03-16 99 views
0

我想用SQLite创建一个待办事项列表,我一直在关注本教程:http://klanguedoc.hubpages.com/hub/IOS-5-How-To-Display-SQLite-Data-in-a-UITableView 但它不工作!模拟器运行,应用程序打开,但表格是空白的。难道我做错了什么?我正在使用xcode 4.2进行雪豹。UITableView没有填充SQLite

在.sqlite文件中,我有一个字符串文本,整数优先级和布尔值完整。不过,我刚刚实现了“文本”,以使事情变得更简单。

这里是我的代码:

// 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:numberOfRowsInSection:返回一个适当的值?什么是tableView:cellForRowAtIndexPath:返回?我会在这两个地方设置一个断点,并检查以确定这些方法甚至被调用。

最后,如果您使用的是.xib文件,是否存在与UITableView的连接?

+0

我明白了,我会尽力 – sillyparrot 2012-03-16 00:40:21

+0

非常感谢您的快速响应。不过,我对xcode很陌生。断点是蓝色箭头是否正确?所以我会把一个蓝色的箭头放在表示返回的行上?值应该显示在底部?因为我刚刚那样做了,但模拟器仍然运行,并且是空的,我什么都看不到。 – sillyparrot 2012-03-16 00:45:48

+0

哦,请等待,我已经研究过如何使用断点 – sillyparrot 2012-03-16 00:55:04