我在特定用户的数据库中有 100 多条记录。现在我只想显示其中的前 30 条,当按下"更多"按钮时,它们下方将出现 30 条记录。如何在iPhone应用程序中执行此操作?
可以使用 SQL 查询来实现目标
-(NSMutableArray *)SelectResult:(int)offset
{
NSMutableArray *arrdata;
arrdata=[[NSMutableArray alloc]init];
sqlite3 *database;
NSString *databasePath = [self getDBPath];
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
NSString *sqlQuery=[NSString stringWithFormat:@"Select * from TableName where RecordId > %d and RecordId <=%d",offset,offset+30];
sqlite3_stmt *compiledStatement;
compiledStatement=nil;
if(sqlite3_prepare_v2(database, [sqlQuery UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK)
{
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
if ((char *)sqlite3_column_text(compiledStatement,0)!=0)
{
NSMutableDictionary *temData = [[NSMutableDictionary alloc] init];
int pKey = sqlite3_column_int(compiledStatement,0);
[temData setValue:[NSString stringWithFormat:@"%d",pKey] forKey:@"RecordId"];
[arrdata addObject:temData];
}
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
return arrdata;
}
请调用此方法并将偏移量作为整数传递,它将为您提供 30 个偏移量
像偏移量 = 0 然后 1 到 30偏移量 = 30,然后 31 到 60 等。
希望对您有所帮助
快乐编码
只需使用NSMutableArray
来保存数据。在表视图中datasource
确保它显示一个额外的单元格(带有"更多"消息),如果有其他记录要显示。
在didSelectRowAtIndexPath
中,您可以通过从数据库添加其他记录来修改表视图的数据数组。
您可以根据数据数组的大小动态生成 SQL 查询。