抱歉,我自己尝试过,但找不到解决方案我的 viewdidload 方法中有以下代码
NSString *docsDir;
NSArray *dirPaths;
dirPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
docsDir=[dirPaths objectAtIndex:0];
databasePath=[[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"database.db"]];
NSLog(@"%@",databasePath);
NSFileManager *filemgr=[NSFileManager defaultManager];
if([filemgr fileExistsAtPath:databasePath]==YES)
{
[self getFinal];
}
else {
NSLog(@"please order");
}
[super viewDidLoad];
它调用一个名为 getFinal 的方法,如下所示
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath,&database)==SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat: @"SELECT * FROM FINALORDER"];
const char *query_stmt = [querySQL UTF8String];
sqlite3_prepare_v2(database,query_stmt,-1,&statement,NULL);
if (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *itemname = [[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)]autorelease];
//itemn.text = itemname;
NSString *qua = [[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)]autorelease];
//quantity.text = qua;
NSString *total = [[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)]autorelease];
//totalcost.text=total;
}
}
sqlite3_finalize(statement);
sqlite3_close(database);
我想以 tableview 格式显示 itemname、qua 和 total。我尝试使用协议并实现委托以及数据源方法,但数据源方法 cellforindexpath 无法识别这些变量,我应该怎么做?谢谢这里是表格视图的代码
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
//I DONT KNOW WHAT TO RETURN HERE..
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
cell.textLabel.text = itemname //i need to get the itemname here
cell.detailTextLabel.text = qua,total //i need the quantity and the totalcost values
return cell;
}