我正在制作一个小型iPhone应用程序,其中我实现了数据库概念,用于存储名称,联系人号码,电子邮件ID。我希望每当我用户保存任何联系人时它都会显示在表格视图中。在 - (IBAction)retriveall:(id)sender 操作我正在从数据库中检索所有数据并将其存储到数组中。现在我想在tableview中显示所有数据。
我该怎么做?请帮帮我。
-(IBAction)retriveall:(id)sender
{
[self retrive2];
for (int i =0; i< [namearray count]; i++)
{
NSLog(@"Name is:%@",[namearray objectAtIndex:i]);
NSLog(@"Password is:%@",[passarray objectAtIndex:i]);
}
}
-(IBAction)retrive:(id)sender
{
[self retrive1];
two.text = databasecolorvalue;
UIAlertView *favAlert=[[UIAlertView alloc] initWithTitle:@"" message:@"Retrived" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[favAlert show];
[favAlert release];
}
-(void)retrive1
{
databaseName = @"med.sqlite";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
@try {
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement="select pswd from Login where username = ? ";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the feeds array
// Create a new animal object with the data from the database
sqlite3_bind_text(compiledStatement, 1, [one.text UTF8String], -1, SQLITE_TRANSIENT);
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
databasecolorvalue =[NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement,0)];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
@catch (NSException * ex) {
@throw ex;
}
@finally {
}
}
答案 0 :(得分:0)
您应该将可变数组声明为实例变量,例如
NSMutableArray * colors;
在viewDidLoad
方法中对其进行初始化,然后更改retrive1
方法以添加到colors数组中。
-(void)retrive1
{
/* Clearing existing values for newer ones */
[colors removeAllObjects]
/* Get database path */
@try {
[..]
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
[colors addObject:[NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement,0)]];
}
[..]
}
@catch (NSException * ex) { @throw ex; }
@finally { }
}
你必须以通常的方式使用数组作为源来实现其余的表视图方法。
答案 1 :(得分:0)
您必须为表视图创建具有UITableViewDataSource协议的所有数据和实现的对象,并将其设置在TableView属性dataSource中。在您调用UITableView方法之后
-(void)reloadData;