【问题标题】:UITableView not getting populated with sqlite database columnUITableView 未填充 sqlite 数据库列
【发布时间】:2023-03-11 00:06:01
【问题描述】:

这是我用来使用 sqlite 数据库中的列的内容填充 UITableView 的方法的代码。但是,这在运行时没有给出错误,但在 UITableView 中仍然没有给出任何数据。如果有人可以提供帮助,将不胜感激。

- (void)viewDidLoad{
    [super viewDidLoad];
    self.title = @"Strings List";
    UITableViewCell *cell;
    NSString *docsDir;
    NSArray *dirPaths;
    sqlite3_stmt *statement;
    // Get the documents directory
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    docsDir = [dirPaths objectAtIndex:0];

    // Build the path to the database file
    databasePath = [[NSString alloc]initWithString: [docsDir stringByAppendingPathComponent:@"strings.sqlite"]];

    NSFileManager *filemgr = [NSFileManager defaultManager];

    if ([filemgr fileExistsAtPath: databasePath ] == NO) 
    {
        const char *dbpath = [databasePath UTF8String];
        if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
        {
        NSString * query =
            @"SELECT string FROM strings";
            const char * stmt = [query UTF8String];
            if (sqlite3_prepare_v2(contactDB, stmt, -1,&statement, NULL)==SQLITE_OK){
                if (sqlite3_step(statement) == SQLITE_ROW)
                {
    NSString *string = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 0)];
                    cell.textLabel.text = string;                }
            }
            else NSLog(@"Failed");
            sqlite3_finalize(statement);
       }
       sqlite3_close(contactDB);
    }

}

【问题讨论】:

    标签: iphone objective-c uitableview ios5 xcode4.3


    【解决方案1】:

    我建议你通读Table View Programming Guide

    你放入viewDidLoad的大部分序列通常放在

    - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath; 
    

    【讨论】:

    • 我知道那是放在里面的,但那是在一个人有一个字符串类来保存可以在这个方法中使用的字符串对象的情况下。我没有创建任何字符串类,这就是我感到困惑的原因:(
    【解决方案2】:

    当你拥有表格视图的所有委托方法时,你正在使用那个相当复杂的方法,这就像把黄油涂在面包上一样。

    试试我的答案Link

    让我知道它是否有效!!!!
    干杯

    【讨论】:

    • @BarinStroms 在你提到的链接中只提到了应用程序,我坚持的事情与 uitableview 有关系:(
    • 我在答案中提到的是有时DB和Xcode存在问题,它可能没有正确连接。除了我所描述的方法之外,在处理数据库时最为人所知。
    【解决方案3】:

    如果您使用模拟器进行测试,请删除之前的构建并再次安装应用程序。它从文档目录中获取 SQL 文件。所以请试试这个,让我知道。谢谢

    【讨论】:

      【解决方案4】:

      您是否检查过问题出在您的 tableview 还是数据库上? 您是否能够从数据库中获取数据?检查您是否缺少任何在代码中连接 sqlite 的方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-09
        • 2019-05-20
        • 2016-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多