【问题标题】:UITableView not populating with SQLiteUITableView 没有填充 SQLite
【发布时间】:2012-03-16 00:35:47
【问题描述】:

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

在 .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;
}
}

【问题讨论】:

    标签: sqlite uitableview ios5 xcode4


    【解决方案1】:

    如果您使用的是标准 UITableView,tableView:numberOfRowsInSection: 是否返回适当的值?什么是 tableView:cellForRowAtIndexPath: 返回?我会在两个位置都设置一个断点,并检查这些方法是否被调用。

    最后,如果你使用的是 .xib 文件,是否有与 UITableView 的连接?

    【讨论】:

    • 非常感谢您的快速回复。但是,我对 xcode 很陌生。断点是蓝色箭头正确的吗?所以我会在表示返回的行上放一个蓝色箭头?值应该显示在底部?因为我刚刚这样做了,但模拟器仍在运行,并且是空的,我什么也看不到。
    猜你喜欢
    • 2023-03-11
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-20
    相关资源
    最近更新 更多