iPhone SDK:从SQLite加载UITableView [英] iPhone SDK: loading UITableView from SQLite

查看:54
本文介绍了iPhone SDK:从SQLite加载UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我对UITableViews以及从SQLite数据库获取/插入数据有很好的了解.我在困扰一个建筑问题.

我的应用程序将3个值保存到数据库中,可以有很多行.但是我会将它们加载到表中吗?

从我看过的所有教程中,有一点可以通过执行SELECT语句将整个数据库加载到NSMutableArray或类似的对象中.

然后在

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

称为,所需的行从先前加载的NSMutableArray(或类似结构)中剔除.

但是我有成千上万的行吗?我为什么要预加载它们?

我应该在每次调用cellForRowAtIndexPath时查询数据库吗?如果是这样,我将用什么作为索引?

表中的每一行都会有一个AUTOINCREMENT索引,但是由于某些行可能会被删除,索引将不对应于表中的行(在SQL中,我可能会遇到这样的情况,其中缺少索引3的行):

1数据1数据1 2数据2数据2 4. data3 data3

谢谢

解决方案

我通过将所有现有db条目中的表单元格从db读取到数据源数组中来解决此问题.但是,这些对象不会从数组中删除,除非需要删除,否则它们会停留在该数组中.

例如,我的一个应用程序读取了1,700行,每行创建一个对象,分配一个NSUInteger(自动递增值)和一个NSString(对象的名称,将在单元格中显示)并放入它们放入数据源数组.这整个过程仅需200-300毫秒-您必须测试10'000 +个条目是否花费太长时间,但对于数千个条目,只需阅读所有内容就可以了.我记得内存消耗也很低,无法查询ATM到底有多少.

然后,当用户点击一行时,我查询数据源数组以找到他刚刚点击的对象,然后该对象从数据库中加载所有其他值,因为它知道自己的数据库密钥,所以可以这样做. id(它本身会水合").

出于完整性考虑(使用 FMDB ):

NSResultSet *res = [db executeQuery:@"SELECT my_key, my_name FROM table"];
while ([res next]) {
    NSDictionary *dict = [res resultDict];
    MyObj *obj = [MyObj obj];
    obj.id = [dict objectForKey:@"my_key"];
    obj.name = [dict objectForKey:@"my_name"];

    [datasourceArray addObject:obj];
}

I think I got a good handle on UITableViews and on getting/inserting data from/to SQLite db. I am straggling with an architectural question.

My application saves 3 values int the database, there can be many/many rows. However would I load them in the table?

From all the tutorials I have seen, at one point entire database is loaded in the NSMutableArray or similar object via performing SELECT statement.

Then when

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

called, rows required are dolled out from the previous loaded NSMutableArray (or similar stracture).

But what i have have thousands for rows? Why would I pre-load them?

Should I just query database each time cellForRowAtIndexPath is called? If so, what would I use as an index?

Each row in the table will have an AUTOINCREMENT index, but since some rows may be deleted index will not correspond to rows in the table (in the SQL I may have something like this with row with index 3 missing):

1 Data1 Data1 2 Data2 Data2 4. data3 data3

Thanks

解决方案

I solve this by reading what the table cell needs from my db into the datasource array, of all the existing db entries. The objects don't get removed from the array however, they stay there unless they need to be removed.

For example one of my apps reads 1'700 rows, for each row creates an object, assigns an NSUInteger (the autoincrement value) and an NSString (the name of the object, which will be displayed in the cell) and puts them into the datasource array. This whole process takes only about 200-300 milliseconds - you'll have to test whether it takes too long for 10'000+ entries, but for some thousand entries it sould be okay to just read it all. I remember that memory consumption is also quite low, can't look up how much exactly ATM.

Then, when the user taps a row, I query the datasource array to find the object he just tapped, and this object then loads all its other values from the database, which it can do since it knows its own database key/id (it "hydrates" itself).

For completeness sake (using FMDB):

NSResultSet *res = [db executeQuery:@"SELECT my_key, my_name FROM table"];
while ([res next]) {
    NSDictionary *dict = [res resultDict];
    MyObj *obj = [MyObj obj];
    obj.id = [dict objectForKey:@"my_key"];
    obj.name = [dict objectForKey:@"my_name"];

    [datasourceArray addObject:obj];
}

这篇关于iPhone SDK:从SQLite加载UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆